Bloomberg LP Interview Question for Software Engineer Interns


Country: United States




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

Take a chasing pointers approach - keep a current pointer to mark position in the array, and a tail pointer such that the start of array to tail is free of commas.

char[] arr = "1,234,34,54";  

char *RemoveCommas(char *arr) { 
	char *curr = arr, *tail = arr; 
         while (*curr) { 
             if (*curr != ',') {
                  if (curr != tail) 
                       *tail = *curr;
                 curr++;
                 tail++;
            } else  
                curr++;     
         }
         *tail = '\0';
         return arr;
}

- pacman March 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess you meant

while (*curr)

Please correct it.

- arunprasath9586 March 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes, you are right. I fixed it.

- pacman March 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How would the code change if the given input array had 2 commas on after the other:

1,,2,345,78

- Girish April 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

my code at that bottom works for any number of commas in it.

- Nishanth April 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I ran the code but got error in this line "*tail = *curr;" The error says:EXC_BAD_ACCESS

- Jascha June 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

Yet another C approach (fully runnable).

This is pretty simple, but note the check for (t < s) may be more wasteful than simply overwriting *t with its own value. There's no harm in that, and you would avoid a conditional.

#include <stdio.h>

void remove_commas(char *s) {
    char *t;
    char c;

    for (t = s; c = *s; ++s) {
        if (c == ',') continue;
        if (t < s) *t = c;
        ++t;
    }
    *t = '\0';
}

int main(int argc, char **argv) {
    char arr[] = "1,234,34,54";
    remove_commas(arr);
    printf("%s\n", arr);
    return 0;
}

- showell30@yahoo.com March 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

Java:

public static void remove(char[] data) {
	int offset=0;
	for (int i=0; i<data.length; i++)
		if (data[i]==',')
			offset++;
		else
			data[i-offset]=data[i];
}

- Anonymous March 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

one liner in Python

arr = "1,234,34,54"
print arr.replace(',','')

C++ Strings

#include <iostream>
using namespace std;
int main() {
    string s = "1,204,342,544";
    s.erase(remove(s.begin(), s.end(), ',' ), s.end());
    cout << s;
}

- arunprasath9586 March 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char arr[] = "1,234,56,7,8,9,11,12";
   char *c = arr;
   char *n = NULL;

   while( *c != '\0') {

       if ( ( *c == ',' ) && ( !n ) ) {
            n = c;
       } else if ( ( *c == ',' ) && n ) {
       } else if ( n ) {
            *n = *c;
            n++;
       }

       c++;
   }

   if ( n ) { *n = '\0'; }

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

in C:

char array[] = "2,234,1,345";
int j =0;
for(int i = 0; i<strlen(array); i++)
{
if(array[i] != ',')
array[j++] = array[i];
}
array[j] = '\0';

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

#include <stdio.h>
#include <string.h>

void main(){
char s[] = "1,234,34,54";
int len=strlen(s);
int i=0;
while( *s != '\0') {
	i++;
	if (*s==',') memmove(s,s+1,len-i);
	s++;
}
}

- 2L August 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

void main(){
	char s[] = "1,234,34,54";
	char *t=s;
	int len=strlen(t)+1;
	int i=0;
	while( *t != '\0') {
		i++;
		if (*t==',') 
			memmove(t,t+1,len-i);
		else t++;
	}
}

- 2L August 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

char* remove_comma(char* str)
{ 
      int n = strlen(str), i(0),  k(0);
     while (i < n)
     {
             if (str[i] == ',') i++;
             else str[k++] = str[i++];
     }
     str[k] = '\0';
     return str;
}

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

public class removeComma {
	public static void main(String args[]){
		String arr = "1,234,34,54";
		StringBuffer a = new StringBuffer();
		for(int i =0;i<arr.length();i++)
		if(arr.charAt(i) != ',')
			a.append(arr.charAt(i));
		System.out.println(a.toString());		
	}
}

- karpagaganesh March 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You will be shifting everything left. Use pointers s, d and copy s into d. Then if s == ',' increase s so that you copy some number into d.

int main() {
    char str[20] = "123,45,6,7,9";
    int d = 0,  s = 0;
    while (str[s] != '\0') if (str[s] == ',') s++; else str[d++] = str[s++];
    while (str[d] != '\0') str[d++] = ' ';
    cout << str << endl;
}

- Ehsan May 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As we go through the string, keep count of comma and use that counter to pick the element to be overwritten.

int _tmain(int argc, _TCHAR* argv[])
{
char array[] = {"1,234,34,54"};
int iLen = strlen(array);
int comma_count = 0;
int i = 0;
while( i < iLen)
{
if(array[i] == ',')
comma_count++;
else
array[i-comma_count] = array[i];
i++;
}
array[iLen-comma_count] = 0;
}

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

public static void main(String[] args) {
		// TODO Auto-generated method stub
		String arr = "1,234,34,54";
		
		StringBuilder input = new StringBuilder(arr);
		
		for(int i = 0; i<input.length(); i++){
			
			if(input.charAt(i) == ','){
				
				input.deleteCharAt(i);
			}
				
		}
		
		System.out.println(input.toString());

	}

- Anil July 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is an Iterative version:

...
	char szText[]="1,234,34,54";
	cout << "Source    text: " << szText << endl;
	size_t iLen = strlen( szText );
	size_t iPos = 0;
	char *pszText = szText;
	while( iPos < iLen )
	{
		if( *pszText++ != ',' )
			iPos++;
		else
		{
			strcpy( &szText[iPos], pszText );
			pszText = &szText[iPos];
			iLen--;
		}
	}
	cout << "Corrected text: " << szText << endl;
...

Output:
Source text: 1,234,34,54
Corrected text: 12343454

- Sergey Kostrov November 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public char[] removeCommas(char[] a)
    {
    	int count=0,n=a.length;
    	for(int i=0;i<a.length;i++)
    	{
    		if(a[i] == ',')
    		{
    			count++;
    		}
    		else
    		   a[i-count]=a[i];
    	}
    	
    	if(count > 0)
        	a[n-count]='\0';
    	return a;

}

- Satish January 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
char s[] = "1234,34,54";
char *p = s-1;
char *q = s + strlen(s);
char *r = NULL;

while (++p < q) {
if (*p == ',') {
r = p;
break;
}
}

if (r != NULL) {
while (++p < q) {
if (*p != ',') {
*r++ = *p;
}
}
*r = '\0';
}
std::cout << s << endl;
return 0;
}

- Kelvin Chung December 05, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

void main()
{
	char arr[] = "1,234,34,54";
	int count = 0;
	int len = strlen(arr);
	char *c = new char[len];
	for(int i=0;i<len;i++)
		c[i] = '\0';
	for(int i =0 ;i<len;i++)
		if(arr[i] != ',')
		{
			c[count] = arr[i];
			count++;
		}
	cout<<c;
}

- Nishanth April 02, 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