Bloomberg LP Interview Question for Interns


Team: Financial Software Developer
Country: United States
Interview Type: Phone Interview




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

/* Reverse a sentence */

#include<iostream>
#include<stack>
#include <stdio.h>
#include <string.h>
using namespace std;

void ReverseASentence(char* str){

char* pch;
stack<char*> strStack;
pch = strtok(str," ");

while(pch!= NULL){

strStack.push(pch);
pch = strtok(NULL," ");
}

while(!strStack.empty()){
cout<<strStack.top()<<" ";
strStack.pop();
}

}

int main(){
cout<<"Hello"<<endl;
char str[]="hello I am a Good Boy";
ReverseASentence(str);
}

- Raj January 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

Question is meaningless with the use of Modern API's in C++/Java.

3 answers
1. Use complex pointers in C to revers each word in place.
2. Push each word on the stack and pop them out.
3. Revers the entire string and than revers indivdual words.

- Abhi January 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

void rev(int l, int r)
{
    char temp;
    while(l<r)
    {
        temp = str[l];
        str[l] = str[r];
        str[r] = temp;
        l++,r--;
    }
}

void fun()
{
    int start = 0, last = strlen(str)-1;
    rev(start, last);
    last = 0;
    while(start < strlen(str))
    {
        while(str[last] != ' ' && str[last] != '\0')
            last++;

        rev(start, last-1);

        while(str[last] == ' ' && str[last] != '\0')
            last++;
        start = last;
    }

}

- ASHISH January 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public static void main(String[] args) {
		
		
		char[] l = "dog world car life".toCharArray();
		System.out.println(l);
		for(int i =0; i < l.length/2; i++){
			char t = l[i];
			l[i] = l[l.length - i - 1];
			l[l.length - i-1] = t;
		}
		
		for(int j = 0, s = 0, e = 0; j <= l.length; j++){
			
			if(j == l.length || l[j] == ' '){
				e = j - 1;
				for(; s < e;){
					
					char t = l[s];
					l[s] = l[e];
					l[e] = t;
					s++;
					e--;
				}
				
				s = j + 1;
				
			}
		}
		System.out.println(l);



}

- marcelovox2 January 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include<iostream>

using namespace std;

void rev(string &s)
{
	int end = s.length() - 1;
	char tmp;
	for(int i=0 ; i < s.length() &&  i < end; i++, end--)
	{
		tmp = s[i];
		s[i] = s[end];
		s[end] = tmp;
	}
	//cout << s << endl;
}
int main()
{
	string s = "This is a question" ;
	string res;
	cout << endl<<s<<endl;
	rev(s);
	string tmp;
	for(int i=0 , j = 0;i <= s.length() ; i++)
	{
		if(s[i] == ' ' || i == s.length() )
		{
			rev(tmp);
			res.append(tmp);
			res.append(" ");
			//resetting
			tmp.clear();
			j=0;
		}
		else
		{
			tmp += s[i];
		}
	}	
	cout << res << endl;
	return 0;
}

- David Billa January 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public static void rev(String str){
		//count the number of the words.
		int count=1;
		for(int i=0;i<str.length();i++){
			if(str.charAt(i) == ' '){
				count++;
			}
		}
		String[] output = new String[count];
		int index = 0;			//the pointer to str
		for(int i=0;i<count;i++){
			char[] tempWord = new char[10];
			int charCount = 0;
			while(index<str.length()&&str.charAt(index) != ' '){
				tempWord[charCount] = str.charAt(index);
				charCount++;
				index++;
			}
			while(index<str.length()&&str.charAt(index) == ' '){
				index++;
			}
			output[i] = String.copyValueOf(tempWord);
		}
		
		int i=0;
		int j=output.length-1;
		while(i<j){
			String temp;
			temp = output[i];
			output[i] = output[j];
			output[j] = temp;
			i++;
			j--;
			
		}
		for(i =0;i<output.length;i++){
			System.out.println(output[i]);
		}
		
	}

- zc236@cornell.edu February 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def reverseString(str): 
   tokens = str.split()    #loses extra whitespace if any in string
   iter = reversed(tokens)
   newstr = ""
   for token in iter: 
      newstr += token 
      newstr += ' '
   return newstr
if __name__ == "__main__":
   print reverseString("i am a cat")

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

in java if you do :

String sentence = "This is a  sentence";
        String[] tok = sentence.split(" ");
        System.out.println(tok.length);

it will print : 5 ,so there are no loses of extra whitespaces

- Kamy January 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

use strtok(string," ");

Space is delimiter.

then apply a for loop backwards and display it

- 59.aditya January 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

void rev_String(char* sptr, char* eptr)
{
while((eptr - sptr) > 0)
{
*sptr = *sptr + *eptr;
*eptr = *sptr - *eptr;
*sptr = *sptr - *eptr;
++sptr;
--eptr;
}
}

int main()
{
char str[] = "India Is Great";
rev_String(str,str+strlen(str)-1);
cout<<str<<endl;
char* sptr = NULL;
char* eptr = NULL;eptr = sptr = str;

while(*eptr != '\0')
{
++eptr;
if(*eptr == ' ')
{
rev_String(sptr,eptr-1);
sptr = eptr+1;
eptr = eptr+1;
}

if(*eptr == '\0')
{
rev_String(sptr,eptr-1);
break;
}
}
cout<<str<<endl;
}

- Bond January 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

haha, why the fancy swapping without a temp variable!, makes your code less readable.

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

public class ReverseSentence{
    
    public static String reverseSentence(String sentenceString){
        char[] rev = new char[sentenceString.length()];           
        char[] sentence = sentenceString.toCharArray();
        
        //traverser for the back
        int wordEnd = sentence.length -1;
        int revPointer = 0;
        for(int i = sentence.length - 1; i >= 0; i --){
            if(sentence[i] == ' '){
                int traverser = i + 1;
                while(traverser <= wordEnd){
                    rev[ revPointer++ ] = sentence[ traverser++ ];
                }
                wordEnd = i - 1;
                rev[revPointer++] = ' ';
            }
        }
        //copy the first word!
        int traverser = 0;
        while(traverser <= wordEnd){
            rev[revPointer ++] = sentence[traverser ++];           
        }
        return new String(rev);
    }

    public static void main(String[] args){
        String sentence = "We will we will rock you";
        System.out.println(ReverseSentence.reverseSentence(sentence));
        return;
    }
}

- Shu January 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverse(char *str,int start ,int len)
{
int i,j;
for(i=start, j=len-1 ;i<=j; i++,j--)
{
char temp;
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
//puts(str);
}
void reverse_fun(char *str, int len)
{
char *ptr;
ptr=&str[0];
int start=0;
int last=0;

while( *ptr!='\0')
{
if(*ptr==' ')
{
reverse(str,start,last);
start=last+1;
}
ptr++;
last++;
}
if(!ptr=='\0')
{
reverse(str,start,len);
}
}

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

public class ReverseArray {


public static void main(String[] args) {
String s = "this is a test";
char[] array = s.toCharArray();
ReverseArray reverseArray = new ReverseArray();
int count = reverseArray.count(array);
StringBuffer[] string = new StringBuffer[count];
for(int k =0;k<string.length;k++){
string[k]= new StringBuffer();
}

int j =0;
for(int i =0;i<array.length;i++){
if(array[i]!= ' '){
string[j].append(array[i]);
}
else{
j++;
}

}
for(int g=0;g<string.length;g++){
System.out.print(" " +string[g]);

}
System.out.println();
for(int g=string.length-1;g>=0;g--){
System.out.print(" " +string[g]);
}
}
//counting the number of elements in the string
public int count(char[] array){
int count=0;
for(int i =0;i<array.length;i++){
if(array[i] == ' '){
count++;
}

}
return count+1;
}

}

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

JAVA CODE:
String m="gentle man";
String[] m1=m.split(" ");
for(i=m1.length-1;i>=0;i--)
System.out.println(m1[i]);

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

With use of pointers it is very easy. Code below in C. Please provide your comments.

void reverse(char *str, int len)
{
    char *p;
    int i;
    p = &str[len-1];
    for(i=len-1; i>=0; i--)
    {
        if(str[i] == ' ')
        {
            printf("word :  %s\n", (p+1));
            str[i] = '\0';
        }
        p--;
    }
    printf("word :  %s\n", ++p);
    
}

int main()
{
  char in[30] = "this is a sentense";
  reverse(in, strlen(in));
}

- Ashish Kshirsagar November 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C++ Fans

void reverseSentence(string str)
{
stringstream strstream(str);
vector<string> splitVect;
string item;

while(getline(strstream,item,' '))
{
splitVect.push_back(item);
}

item.clear();
for(int i = splitVect.size()-1; i >= 0; i--)
{
item += splitVect[i] + ' '; //Its OK. I ignored the last extra space
}

cout<<item<<endl;
}

- GV December 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverseSentence(string str)
{
	stringstream strstream(str);
	vector<string> splitVect;
	string item;

	while(getline(strstream,item,' '))
	{
		splitVect.push_back(item);
	}

	item.clear();
	for(int i = splitVect.size()-1; i >= 0; i--)
	{
		item += splitVect[i] + ' ';  //I just ignored the last white space
	}

cout<<item<<endl;
}

- GV December 16, 2016 | 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