Bloomberg LP Interview Question for Financial Software Developers






Comment hidden because of low score. Click to expand.
1
of 0 vote

i=0,j=0
while(s[j])
{
if(s[j]!=' ')
s[i++]=s[j];
j++;
}
s[i]='\0';

- Anonymous November 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i do agree that logic is simpler here than 'esoteric' bodyless loops above :)

One difference between what i hve above vs. this is that if there are no spaces in the string, above code does no copying, whereas as this one copies each character onto itself. Though not a big deal, it can easily fixed by adding a loop ahead to place i at the first space.

- acoader November 13, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

s/[\s]+//g

- Anonymous September 22, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL, if PERL is allowed, I hope.

- TimeVision December 01, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

infact,

sub callSpacePurge {
$_ =shift;
s/[\s]//g;
return $_;
}

- Anonymous September 22, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Question is kinda unclear....But from what I understand, the idea is to move the spaces to the end of the string, inplace and then copy all the valid input to a new string and return that. Delete the old string. I believe this is his idea of inplace...

- nanmaga October 30, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

in place means don't use any temporaries. Do the task directly in the original object.

In this case the question seems to be to remove the spaces - not moving them...

- acoader November 07, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Idea would be to find first space (using p), and from then on move every non space chacacter to the writable position, which is p,p+1,p+2,...

void removeSpaces(char *p) {
assert(p);
while (*p && *p++ != ' ' ); // move p just past first space
char *q = p; // p,q both point past first space
while ( --p, *--q ) { // q points to first space, p to next writable pos
while (*++q == ' ' ); // move q to next non-space
while (*q && (*p++ = *q++) != ' '); // copy, moving q past next space
}
}

- acoader November 08, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Scratch the above...
Heres a bug free one :)

void removeSpaces(char *p, char blank) {
assert(p);

// following 2 lines position p to first space or null
while (*p++ != blank && *(p-1));
p--;

char *q=p;

while ( *q ) {

// position q to next non-space
while (*q && *++q == blank);

// copy q to p, until null gets copied or while q is
// non-space
while ( *q != blank && (*p++ = *q++) );
q--; // needed to ensure q is in array bound
}
}

- acoader November 08, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void RemoveSpaces(char* str)
{
if(!str) return;

int read = 0, write = 0;

//find first space character
while(str[write] != ' ')
{
if(str[++write]) continue; //there is a space

return; //there are no more space characters
}

read = write; //there were no spaces so far so read here onwards

do{

//find first non-space character
while(str[read] == ' ')
{
if(str[++read]) continue; //there is a non-space character

str[write] = '\0';
std::cout << "\n Modified String is: " << str << "\n";
return; //there are no more non-space character
}

str[write++] = str[read];
str[read++] = ' ';

//find first space character
while(str[write] != ' ')
{
if(str[++write]) continue; //there is a space

return; //there are no more space characters
}

}while(str[read]);

str[write] = '\0';
std::cout << "\n Modified String is: " << str << "\n";
}

- CAG January 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i can not understand ur code

- sumit khoja April 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

void main()
{
char *s = "h e l l o";

int i = 0, j = 0;

while((s[i] != '\0') && (s[i] != ' '))
{
i++;
j++;
}
while(s[j] != '\0')
{

while(s[j] != '\0' && s[j] == ' ')
j++;
while(s[j] != '\0' && s[j] != ' ')
{
s[i++] = s[j++];

}
}
*(s+i) = '\0';
printf("%s\n", s);


}
working code

- pisasu January 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <string>

std::string & removeSpaces( std::string & str )
{
std::string::iterator to = str.begin();
for( std::string::const_iterator from = str.begin(); '\0' != *from; ++ from )
{
if ( ' ' != *from )
*(to ++) = *from;
}

*to = '\0';
return str;
}

- Anonymous February 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void removeSpaces(char * str){

int write=0;
if (!str)
return;


for (int read=0; read < strlen(str); ++read){
if ((str[read] != ' ') && (str[read] != '\t') && (str[read] != '\n') && (str[read] != '\r')){
str[write++]=str[read];
}
}
str[write]='\0';


}

- Anonymous March 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using std::cout;
using std::endl;

void removeSpace(char * str)
{
	char* current = str;
	if(str == NULL)
		return;
	if(*str == ' ')
	{
		//skip all the empty spaces
		do
		{
			str++;;
		}while(*str==' ');
	}

	while(*str != '\0')
	{
		if(*str==' ') 
		{
			*current = ' ';
			current++;
			//skip all the empty spaces
			do
			{
				str++;
			}while(*str==' ');

		}
		else
		{
			*current = *str;
			current++;
			str++;
		}


	}
	//check to see if the last characater was space
	if(*(current-1) == ' ')
		*(current-1) = '\0';
	else
	{
		*current = '\0'; 
	}

}

int main()
{
	char str1[20] = "  hello  world !!! ";
	char str2[30] = "hello     world  !!!";
	char str3[30];
	removeSpace(str1);
	std::cout << str1<<std::endl;
	removeSpace(str2);
	std::cout << str2<<std::endl;
	std::cin.getline(str3, 30, '\n');
	removeSpace(str3);
	std::cout << str3<<std::endl;
	getchar();
}

- Anonymous May 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

.plz keep clear description.

- jaga June 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I'm not sure why other solutions here are so ... complicated.

A simple solution in C

char* removeSpaces(char[] str)
{
    if (str)
    {
        char* pWrite = str;
        for (char* pRead = str; *pRead; pRead++)
            if (*pRead!=' ')
                *(pWrite++) = *pRead;
        *pWrite = 0;
    }
    return str;
}

A solution in D

dchar[] removeSpaces(dchar[] str)
{
    auto write = str;
    foreach(c; str)
        if (c!=' ')
        {
            write.front = c;
            write.popFront;
        }
    return str[0..$-write.length];
}

If the interviewer generalizes the question, more generic solutions might be helpful.
Here again in D

E[] removeItem(E)(E[] r, E itemToRemove)
{
    auto write = r;
    foreach(item; r)
        if (item!=itemToRemove)
        {
            write.front = c;
            write.popFront;
        }
    return r[0..$-write.length];
}

E[] remove(alias predicate, E)(E[] r)
{
    auto write = r;
    foreach(item; r)
        if (predicate(item))
        {
            write.front = c;
            write.popFront;
        }
    return r[0..$-write.length];
}

Then, removeSpaces can be implemented simply as

dchar[] removeSpaces(dchar[] r)
{
    return removeItem(r, ' ');
    // or
    return remove!(x => x!=' ')(r);
}

But I think that might be beyond the scope of the question ;-)

- Leif E Glacer August 09, 2013 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More