Bloomberg LP Interview Question for Financial Software Developers






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

void convert(char *str)
{
    int i = 0, j=0, n = strlen(str);
    while(str[i])
    {
        if(str[i] != ',')
            str[j++] = str[i];
        i++;
    }
    str[j] = '\0';
            
}

- Dee April 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

search the string for comma, whenever it is found, copy the value from next character into comma.

- Anonymous April 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The O(n) solution is:

#include <iostream>
using namespace std;

int main() {
int count = 0, i = 0;
char num[] = "34,00,500";

for(i=0 ; i<9; ++i)
{
if(num[i] == ',') {
count++;
continue;
}
if(count >0) {
num[i-count]=num[i];
}
}
num[i-count]='\0';
cout << "The String is : " << num << endl;
}

- Venkat June 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int nLen = strlen(str);
char* pWriteTo = str;
char* pReadFrom = str;
for (int i = 0; i < nLen; i++)
{
  if (*pReadFrom != ',')
  {
    *pWriteTo = *pReadFrom;
    pWriteTo++;
  }
  pReadFrom++;

}

- NewStart April 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this is a good method ..but the problem is how to decrease the size of char*

- Anonymous April 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int nLen = strlen(str);
char* pWriteTo = str;
char* pReadFrom = str;
for (int i = 0; i < nLen+1; i++)
{
  if (*pReadFrom != ',')
  {
    *pWriteTo = *pReadFrom;
    pWriteTo++;
  }
  pReadFrom++;

}

this will move the null as well :)

- NewStart April 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it is better to use while loop rather than for loop because for loop will use extra memory

- next candidate April 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is not in-place. Plus, extra memory is used.

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

The above won't work: (1) p is in data segment and can't be written to; (2) ignored the cumulating ','; (3) didn't end the new string properly;

Should be:

char p[] = "345,000,000";

printf("%s\n", p);

char* pReadFrom = p;
char* pWriteTo = p;

while(*pReadFrom != '\0')
{
if (*pReadFrom != ',')
*pWriteTo++ = *pReadFrom;

pReadFrom++;
}

*pWriteTo = '\0';
printf("%s\n", p);

- zjxu008 April 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

just replace the commas with space character and you will get the same string without commas

- vikram rathode April 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice lateral thinking

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

int main()
{
    string number = "345,000,000";
    for(int i=0;i<number.length();i++)
    {
            if(number.at(i) == ',')
            {
             number.erase(i, 1);
             number.insert(i, " ");                
            }
    }
    cout<<number<<endl;
   
    return 0;

- Grub April 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

string::iterator sit = str.begin();
string::iterator nit;
for(sit;sit!=str.end();sit++) {
nit=sit;
if (*sit == ',') {
cout<<"sit = "<<*sit<<" nit = "<<*nit<<endl;
nit++;
cout<<"sit = "<<*sit<<" nit = "<<*nit<<endl;
*sit=*nit;
cout<<"sit = "<<*sit<<" nit = "<<*nit<<endl;
}
}

- Anonymous April 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

- char * header memory is used to create a pointer. There is no memory used to store data characters.

void Remove(char *inputParam, char delimitor) {
    char *header = inputParam;
    while(inputParam){
        if(*inputParam != deimitor){
            *header = inputParam;
            header++;
         }
        inputParam++;
    }
    *header = 0;
}

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

int main()
{
char buf[] = "345,678,895";
printf("buf = %s\n", buf);
int ctr=0, len = strlen(buf);
for(int i=0; i<len; ++i)
{
if(buf[i]==','){
++ctr;
memmove(&buf[i], &buf[i+1], strlen(&buf[i+1]));
}
}
buf[len - ctr] = '\0';
printf("buf = %s\n", buf);
}

$ ./a.out
buf = 345,678,895
buf = 345678895

- Anonymous May 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

ah.. this might be O(n^2) instead of O(n) because memmove might be doing a loop also.

- Anonymous May 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

maybe O(n!). Anyway, memmove() and strlen() inside the loop - forget about O(n).

- Anonymous June 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void RemoveComma(char *str){

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

- Anonymous June 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is correct

- Anonymous June 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yup, this is right. This is what I told in the interview and the interviewers seemed satisfied with the answer.

- Bandicoot December 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how can this be correct?? j is set to 0th location of str, so first time the ',' appears it will overwrite the first location with a ',' . the final str would just be of commas

- Mandar December 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

oops.. my bad. neglect the previous comment ! :P

- Mandar December 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="c++" line="1" title="CodeMonkey65210" class="run-this">#include<iostream>
#include<string>

using namespace std;

int main()
{
string s;

cout<<"enter\n";
cin>>s;

int pos,i=0;

while(1)
{


pos=s.find(',',i);

if(pos==-1)break;

// cout<<pos;

s.erase(s.begin()+pos);

i=pos;
}

cout<<s;

return 0;
}</pre><pre title="CodeMonkey65210" input="yes">3,500,000
</pre>

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

int main()
{
char num[] = "123,456,789";
int i = 0, commaCtr = 0;
while(num[i] != '\0')
{
if(num[i] == ',')
{
++commaCtr;
}
else
{
num[i-commaCtr] = num[i];
}
i++;
}
num[i - commaCtr] = '\0';
return 0;
}
1.The count of number of commas is required.
2.Every character must be moved left 'no of commas seen' times.
3.Works for any number of commas present anywhere inside the number.

- Vinay February 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this one?

void remove_commas_inplace(char *src)
      {
            char *next = src;
            while(next && *next)
            {
                  if(*next != ',')
                      *src++ = *next;
                  ++next;
            }
            *src = '\0';
      }

- Optimizer February 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this one?

void remove_commas_inplace(char *src)
      {
            char *next = src;
            while(next && *next)
            {
                  if(*next != ',')
                      *src++ = *next;
                  ++next;
            }
            *src = '\0';
      }

- Optimizer February 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

int main() {
int count = 0, i = 0;
char num[] = "34,00,500";

for(i=0 ; i<9; ++i)
{
if(num[i] == ',') {
count++;
continue;
}
if(count >0) {
num[i-count]=num[i];
}
}
num[i-count]='\0';
cout << "The String is : " << num << endl;
}

- Venkat June 16, 2012 | 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