Bloomberg LP Interview Question for Software Engineer / Developers






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

#include <iostream>
using namespace std;

void Print(char* c, int n) {
  int cur = 0;
  while (cur < n) {
    int end = cur;
    while (end+1 < n && c[end+1] != '_')
      end++;

    for (int i=end; i>=cur; i--)
      printf("%c", c[i]);
    printf("."); // optional
    
    cur = end + 1;
    while (cur < n && c[cur] == '_')
      cur++;
  }
}

int main(){
  char *c = "abc__def___gh_xyz";
  Print(c, strlen(c));
  
  system("pause");
  return 0;
}

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

succinct solution

- orangetime23 May 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool reverseWords( char str[] ){
char *buffer;
int tokenReadPos, wordReadPos, wordEnd, writePos = 0;

/* Position of the last character is length - 1 */
tokenReadPos = strlen(str) - 1;

buffer = (char *) malloc(tokenReadPos + 2);
if( !buffer )
return false; /* reverseWords failed */

while( tokenReadPos >= 0 ){

if( str[tokenReadPos] == ' ' ){ /* Non-word characters */
/* Write character */
buffer[writePos++] = str[tokenReadPos--];

} else { /* Word characters */
/* Store position of end of word */
wordEnd = tokenReadPos;

/* Scan to next non-word character */
while( tokenReadPos >= 0 && str[tokenReadPos] != ' ' )
tokenReadPos--;

/* tokenReadPos went past the start of the word */
wordReadPos = tokenReadPos + 1;

/* Copy the characters of the word */
while( wordReadPos <= wordEnd ){
buffer[writePos++] = str[wordReadPos++];
}
}
}
/* null terminate buffer and copy over str */
buffer[writePos] = '\0';
strcpy(str, buffer);

free(buffer);

return true; /* ReverseWords successful */
}

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

//Output: final string : abc__def_987654321_xyz_123abc_hi

#include <iostream>
#include <string.h>
using namespace std;
//!function to reverse string from strt to end
void reverseStr(string &s, int strt, int end) {
	while(strt<end){
		s[strt] = s[strt]+s[end];
		s[end] = s[strt]-s[end];
		s[strt] = s[strt]-s[end];
		++strt;--end;
	}
}
void reverseBuckets(string &str){
	int start=0,end=0,tmp=0,sz = str.size();
	size_t pos;
	while(end<sz){
		pos = str.find_first_of("_",start);
		if(pos>0)
		if(pos>=sz && end<sz)
			end = sz;
		else
			end = pos;
		reverseStr(str,start,end-1);
		if(end>=sz)
			break;
		pos = str.find_first_not_of("_",end);
		if(pos>0){
			end = pos;
			start=end;
		}++end;
	}
}
int main(){
	string s = "cba__fed_123456789_zyx_cba321_ih";
	reverseBuckets(s);
	cout<<"final string : "<<s<<endl;
	return 0;
}

- blueskin.neo November 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If we're allowed extra space, we can keep reading the string (and pushing each character into a stack) until we encounter a space.. at this point, we replace the characters in the address pointed by a pointer ptr(which initially points to the beginning of the string) by popping characters off the stack. we continue this process until the original string is exhausted.

- Jester January 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <cstdlib>
#include <iostream>

using namespace std;


int main(int argc, char *argv[])
{
char temp;
char arr[]= "xyz_pqr_abc";
int i=0,j=0;
while(j<11)
{
// cout << i << endl;
i=j;
cout << "i= " << i << endl;
while(arr[j]!='_' && j < 11)
{
// cout << j << endl;
j++;
cout << "j =" << j << endl;
}
j=j-1;
int k;
for(k=i;k<=(j+i)/2;k++)
{
printf("%d ",k);
temp = arr[k];
arr[k] = arr[j-k+i];
arr[j-k+i]=temp;
}
cout << endl;
j=j+2;
}
cout << arr << endl;
system("PAUSE");
return 0;
}

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

Here is an Iterative version:

...
	char szText[]="xyz___pqr___abc";
	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--;
		}
	}
	iLen = strlen( szText );
	char t;
	for( size_t i = 0; i < iLen; I += 3 )
	{
		t = szText[i];
		szText[i] = szText[i+2];
		szText[i+2] = t;
	}
	cout << "Corrected text: " << szText << endl;
...

Output:
Source text: xyz___pqr___abc
Corrected text: zyxrqpcba

- Sergey Kostrov November 04, 2014 | 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