Bloomberg LP Interview Question for Software Engineer / Developers






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

A finite automaton should work for this, which seems overkill and silly after I wrote the code :-(

2 important states, SPACE and NOTSPACE.

// Assume NULL terminated string.

#define BEGIN 0
#define SPACE 1
#define NOTSPACE 2
#define END 
// Dont really need the END State.

void EmitSequence(char *s, int state) {

    if (s == NULL) {
        state = END;
        return;
    }

    char curChar = *s;

    
    case (state) {
        BEGIN:
            if (curChar == ' ') {
                state = SPACE;
            }
            break;
        SPACE:
            if (curChar == ' ') {
                state = SPACE;
                break;
            }
            // saw a character. Emit a '(' ')'
            putchar('(');
            putchar(curChar);
            putchar(')');

            state = NOTSPACE;
            break:
        NOTSPACE:
            if (curChar == ' ') {
                state = SPACE;
                break;
            }
            // saw a character. Emit a '(' ')'
            putchar('(');
            putchar(curChar);
            putchar(')');
    }
    EmitSequence(s++, state);
}

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

get some powder!

- cableGuy August 11, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

my $s = shift;  # read in string from the command line
$s =~ s/\s+//g;  # replace all the white spaces in the string with nothing
$s = '(' . $s . ')';  # add the brackets on either side of the string 

print "\n";
print $s; 
print "\n"; 
print $s;

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

1) Replace all space with ")("
2) Put '(' at begining and ')' at the end

O(n) solution

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

What if we have multiple spaces together....

- Min September 23, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

when the first space after any word is encountered put ")(" then use a while loop to eat up as many whitespaces as possible(ie until the first non-whitespace character is encountered).

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

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

T: your solution works flawlessly...and its O(n)...You rock buddy!!!

- LOLer August 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ladka heera hai !!!

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

#include <cstdio>
#include <memory>
#include <cstdlib>
#include <cstring>

int main()
{
char arr[] = "a bc defga dcbaifk";

// Allocate enough space if there is space
// if there is space between each of the chars.
int len = strlen(arr)*3 + 1;

char * q, *p , *qstart;
p = arr;

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

q = (char *) malloc (sizeof(char) * len);
qstart = q;

*q = '('; q++;

while(p && *p)
{
if(*p == ' ')
{
*q = ')';
q++;
*q = '(';
}
else
{
*q = *p;
}

++q;
++p ;
}

*q = ')';
++q;
*q = '\0';

printf(" End q = %s\n",qstart);
}

host:~/testing$ ./replspc
Start p = a bc defga dcbaifk
End q = (a)(bc)(defga)(dcbaifk)

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

string Replace(const string& input)
{
   string ret = "";
   bool in_word = false;
   for (int i = 0; i < input.size(); ++i) {
     if (!in_word && input[i] != ' ') {
         in_word = true;
         ret += '(';
         ret += input[i];
     } else if (in_word && input[i] == ' ') {
         ret += ')';
         in_word = false;
     } else if (in_word && input[i] != ' ') {
         ret += input[i];
     }
   }
   if (in_word) {
      ret += ')';
   }
   return ret;
}

}

- seek January 23, 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