Bloomberg LP Interview Question for Financial Software Developers






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

string removeExtraSpaces( const string& s )
{
	string ret;
	size_t start;
	size_t end;
	start=s.find_first_not_of(" ");
	while ( start != string::npos )
	{
		end = s.find_first_of( " ", start );
		if ( end == string::npos )
		{
			end = s.length();
		}
		ret += s.substr( start, end-start ) + ' ';
		start=s.find_first_not_of(" ", end);
	}
	return ret;
}

- Anonymous July 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

could you explain with example (about extra space removal)?

- anon November 10, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

char* removeExtraSpaces(const char* str) {
    const char* SPACE = " ";
    int len = strlen(str) + 1;
    char* buf = new char[len];
    char* s = new char[len];
    char* t = NULL;

    strcpy(buf, str);
    t = strtok(buf, SPACE);

    if (strcmp(str, t) == 0) return t;
    do strcat(s, t); while ((t = strtok(NULL, SPACE)) && strcat(s, SPACE));

    delete[] buf;
    return s;
}

- Anonymous July 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

u are returning pointer to de-allocated string.

- anonymous December 27, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

char* removeExtraSpaces(const char* str) {
    if (str == NULL) return NULL;
    int len = strlen(str) + 1;
    char* buf = new char[len];
    strcpy(buf, str);
    if (len == 1) return buf;
    const char* SPACE = " ";
    char* s = new char[len];
    char* t = NULL;
    t = strtok(buf, SPACE);
    if (strcmp(str, t) == 0) return t;
    do strcat(s, t); while ((t = strtok(NULL, SPACE)) && strcat(s, SPACE));
    delete[] buf;
    return s;
}

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

string removeSPACE(string text)
{
       int i;
       int c=0;
       for(i=0;i<text.size();i++)
       {
         
         if(text[i]==' ' && c>=1)
         {
         text.erase(i,1);
         i--;
         continue;
         }
          
         else if(text[i]==' ')
         c++;
         
         else if(text[i]!=' ')
         {
         c=0;
         continue;
         }
         
         }
         
       return text;
       
       }

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

void  removeSpace(char* s){
  int hop_forward_steps = 0;

  while(*s != '\0'){
    s++;
    if(*s == ' ' && *(s-1) == ' '){
      hop_forward_steps++;
      continue;
    }
    //jump forward by hop_forward_steps
    *(s-hop_forward_steps)=*s;
  }

  *(s-hop_forward_steps) = '\0';
}

- Miles July 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

any comment welcomed!
my email: milesli@yahoo.com

- Miles July 30, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool removeExtraSpace(char c)
{
static bool isPrevSpace = true;
bool retCode = false;

if ((c == ' ') && isPrevSpace)
{
retCode = true;
}
else
{
retCode = false;
}
if (c == ' ')
{
isPrevSpace = true;
}
else
{
isPrevSpace = false;;
}
return retCode;
}

//class Test
int _tmain(int argc, _TCHAR* argv[])
{
std::string realStr;
std::getline(std::cin,realStr);
realStr.erase(std::remove_if(realStr.begin(),realStr.end),removeExtraSpace),realStr.end());

return 0;
}

- BP August 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

std::string stripWhiteSpace(std::string inputString_){
std::string::size_type pos;
pos = inputString.find_first_of(" ");
while(pos != std::string::npos){
inputString.erase(pos,static_cast<std::string::size_type> (1));
pos = inputString.find_first_of(" ");
}
}

- Rahul August 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

typo:
std::string stripWhiteSpace(std::string inputString)

- Rahul August 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string removeExtraSpaces (const string instr) {
string ret;
string::size_type nonspace = instr.find_first_not_of (" ");
string::size_type nextDoubleSpace;
while (nonspace != instr.npos) {
nextDoubleSpace = instr.find (" ", nonspace);
if (nextDoubleSpace == instr.npos) {
ret.append (instr.begin() + nonspace, instr.end());
} else {
ret.append (instr.begin() + nonspace, instr.begin() + nextDoubleSpace+1);
}
nonspace = instr.find_first_not_of (" ", nextDoubleSpace);
}
return ret;
}

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

char* remove_spaces(char* str)
{
	char* ptr1 = str;
	int i = 0;
	char* new_str = (char*)malloc(sizeof(str));
	if (new_str==NULL)
		exit(1);
	bool start = false;
	char* tmp = new_str;
	while(!start)
	{
		if(*ptr1 == 32)
		{
			ptr1++;
			continue;
		}
		start = true;
		strncpy(tmp,ptr1,1);
		tmp++;
		ptr1++;
	}
	while(*(ptr1 + 1) != '\0')
	{
		if(*ptr1 == 32 && *(ptr1 + 1) == 32)
		{
			ptr1++;
			continue;
		}
		strncpy(tmp,ptr1,1);
		tmp++;
		ptr1++;
	}
	*tmp = '\0';
	return new_str;
}

- Avatar Ang September 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The above code also takes care of the leading and trailing spaces.

- Avatar Ang September 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void remove_spaces(char* str)
{
if(str==NULL) return;
int pos =0;
bool firstblank;
char * current=str;
while(current)
{
if(*current!=''))
break;
current++
}
while(current)
{
if(*current!='')
{
str[pos++]=*current;
firstblank = true;
}
else
{
if(firstblank)
{
str[pos++]=*current;
firstblank = false;
}
}
current++;


}
str[pos]=0;
return;
}

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

void remove_spaces(char* s)
{
	int len = strlen(s);
	char* dest = (char*) malloc(len+1); 
	*dest = '\0';
	char* delimiter = " ";
	char* p = strtok(s,delimiter); 
	if( p != NULL )
	{
		do
		{
			strcat(dest,p);
			strcat(dest," ");
		}while( p = strtok(NULL, delimiter) ); 
	}
	//remove the last space
	dest[strlen(dest)-1] = '\0';
	strcpy(s,dest); 
}

- Girish September 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char * remove_tokens(char a[])
{
char * ptr= new char[strlen(a)];
char * result= new char[strlen(a)];
strcpy(result,"");
ptr= strtok(a," ");
while (ptr)
{

strcat(result,ptr);
strcat(result," ");
ptr= strtok(NULL," ");

}
return result;
}

- Varun October 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Should we implement only thru pointer manipulations manually, or, can we use built-in functions like strtok, strcpy, srtcat, string.erase, string.substring etc...

- Helper December 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

only using pointer manipulations:

#include <stdio.h>
#include <assert.h>

char* remove_extra_space(char* stringIn)
{
assert(*stringIn != '\0');
char *p = stringIn;
char *q = p;
int newSpace=1;

while( *q != '\0') {
if(*q == ' ' && newSpace == 1) {
*p=*q; p++; q++; newSpace=0;
}
else if(*q == ' ' && newSpace == 0){
q++;
}
else if(p!=q) {
*p = *q; p++; q++; newSpace=1;
}
else {
p++; q++; newSpace=1;
}
}

*p = *q;

return stringIn;
}

int main() {
char a[]=" sdfaiyher ius dfa ere ";
//char a[]=" ";
printf("old string=%sEND\n",a);
printf("new string=%sEND\n",remove_extra_space(a));
}

- Anonymous January 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can someone please test this rigorously. I have tested it as much as i can and it works fine

public void removespaces(String s){
char[] str = s.toCharArray();
String tgt = "";
int p1 = 0 , trgt = 0;
while(p1<s.length()){

if(str[p1]==' '){
tgt+=str[p1];
while(str[p1]==' '){
p1++;
if(p1== s.length())break;
}
}
if(p1== s.length())break;
tgt+=str[p1];
p1++;

}
System.out.println(tgt);
}

- Anonymous January 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void remove_Extraspaces(char* str1)
{

int i = 0, j=0, k = 0, write = 0;


while(str1[i]!='\0')
{

while(str1[i] == ' ')
{
i++;
flag = 1;
}

if(flag == 1)
i = i-1;


str1[write++] = str1[i++];

}

str1[write] = '\0';


}

- prep4Inter February 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

please discard last version ...
void remove_Extraspaces(char* str)
{

int i = 0, write = 0, flag = 0;


while(str[i]!='\0')
{
flag = 0;
while(str[i] == ' ')
{
i++;
flag = 1;
}

if(flag == 1)
i = i-1;


str[write++] = str[i++];

}

str[write] = '\0';


}

- prep4Inter February 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void remove_Extraspaces(char* str)
{

        int i = 0, j=0, k = 0, write = 0 flag  =0;

        
        while(str[i]!='\0')
        {
                flag = 0
                while(str[i] == ' ')
                {
                        i++;
                        flag = 1;                        
                }

                if(flag == 1)
                   i = i-1;

                
                str[write++] = str[i++];

        }

        str[write] = '\0';


}

- Anonym February 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

my $str = "this   is   a    string   with multiple         spaces";
$str =~ s/ +/ /g;

- Anon February 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

why everyone are posting their own codes to waste space

- ss July 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char* removeExtraWhiteSpaces(const char* str)
{
int len = strlen(str);
char* return_str = new char[len+1];
if(len <= 1)
return strcpy(return_str, str);

int count = 0;
char next, current = *str;
while(*str != '\0')
{
next = *(str+1);
if (! (current == ' ' && next == ' ') )
{
return_str[count++] = current;
current = next;
}
++str;
}
return_str[count] = '\0';
return return_str;
}

- dellaxim July 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Repost with indentation

char* removeExtraWhiteSpaces(const char* str)
{
  int len = strlen(str);
  char* return_str = new char[len+1];
  if(len <= 1)
    return strcpy(return_str, str);

  int count = 0;
  char next, current = *str;
  while(*str != '\0')
    {
      next = *(str+1);
      if (! (current == ' ' && next == ' ') )
        {
          return_str[count++] = current;
          current = next;
        }
      ++str;
    }
  return_str[count] = '\0';
  return return_str;
}

- dellaxim July 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

const char * removeExtraSpaces(char * str)
{
if (!str) {
return str;
}
if(strlen(str)==1)
{
return str;
}
int read=1;
int write=0;

while (str[read]!='\0') {

if (str[read]==' '&&str[write]==' ') {
read++;
}
else {
write++;
str[write]=str[read];
read++;
}
}

str[++write]='\0';
return str;
}


Time complexity: O(n). I am not sure if this is a solution

- aaron lu October 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <stdlib.h>
//#include <string.h>
int main () {
        char *s = "             This     is        an    example                  ";
        char r[100];

        int i = 0;
        printf("%s\n",'\0');
        while (*s){ //!= '\0') {
                if (*s != ' ') {
                        r[i] = *s;
                        i++;
                }
                else if(i > 0&& r[i-1] != ' '){
                        r[i] = ' ';
                        while((*(++s)) && (*s) == ' '){}
                        i = (*s)? i+1 : i;
                        s--;
                        //i++;
                }
                s++;
        }
        r[i] = '\0';

        printf ("Result:%s", r);
        printf("good\n");
}

- Yiran Qin March 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Input: ");
String input = sc.nextLine();
String [] parts = input.split("\\s+");
String output="";
for(int i=0;i<parts.length;i++)
{
output+= parts[i]+" ";
}
System.out.println(output);
}
}

- AJ January 17, 2015 | 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