Goldman Sachs Interview Question for Financial Software Developers


Country: India
Interview Type: Phone Interview




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

I think the C code will be Okay to remove all spaces in the string :input

int i = 0 ;
for (int j = 0 ;j <strlen(input);++j){
     if(input[j] != ' ')
       input[i++] = input[j];
}
input[i] = 0;

- notbad July 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Even more concise:

for (int i=0, j = 0 ;j <= strlen(input); ++j){
     if(input[j] != ' ')
       input[i++] = input[j];
}

Since the null terminator is also not equal to space, no need to have a special case for it.

- eugene.yarovoi July 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

why can't we remove spaces while reading the string itself

reading a string:
  if input element is " ":
     don't include it in the string:
print string

- rakeshnitcalicut March 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
6
of 6 vote

char[] data = {'a','a','b',' ',' ','c',' ','d','e'};
String s = new String(data);
s = s.replaceAll(" ", "");
System.out.println(s);

- benchan August 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

In C++, I always tray to used a developed algorithm instead of implementing a new one. And to remove a character nothing better than std::remove algorithm.

#include <iostream>
#include <algorithm>
 
int main()
{
    char str[] = "aab  c  de";
    std::remove(str, str+sizeof(str)/sizeof(str[0]), ' ');
    std::cout << str;
}

- jjmmg August 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why not just use strlen(str) instead of "sizeof(str)/sizeof(str[0])" ?

- jeremy November 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

strlen will iterate through string until it find 0, so it performance is worse than sizeof version which is compile time

- Marcin December 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

why can't we remove spaces while reading the string itself

reading a string:
  if input element is " ":
     don't include it in the string:
print string

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

public void removeSpaces(String str) {
int len = str.length();
for (int i = 0; i < len; i++) {
int pos = str.charAt(i);
// 32 is ascii value for Space
if (pos != 32) {
System.out.print((char) pos);
}

}
}

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

do we need to just print the output ? or modify that character array to remove the space?

- cobra July 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

shifting the characters seems better here,than taking a new array and only filling it with desired characters.

- sujita July 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char temp[] = {'a',' ','a',' ','b',' ','c'};
String sb= new String();
for (int i = 0; i < temp.length; i++) {
if(temp[i]!=' '){
sb=sb+temp[i];
}
}

System.out.println(sb.toString());

- raghu.nagabandi July 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void rm_space(char [] ar){

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

System.out.println(new String(ar,0,j));
}

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

char []a = {'a',' ','b',' ','c'};
StringBuffer str = new StringBuffer();
for (int i=0; i<a.length; i++){
if (a[i] != ' ')
str.append(a[i]);

}

System.out.println(str);

- sudhir shetty July 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

i am just trying it other way. let me know if i am approaching wrong way.
#include<stdio.h>
main()
{
char A[] ={'a','a','b',' ',' ','c',' ','d','e'};
int i=0;
int p=0;
int count = 0;
int length=sizeof(A)/sizeof(A[0]);
printf("Original array is:: %s\n and length of array is %d\n",A,strlen(A));
while(i<length)
{
if(A[i] == ' ')
{
if(count<1)
p = i;
count++;
}
else
{
if(count>=1)
{
A[p]=A[i];
p++;
count--;
}
}
i++;
}
A[p] = '\0';

printf("Final array is :: %s \n and legth of final string is %d\n",A,strlen(A));

getch();
}

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

char ch[] = new char[] { 'a', 'a', 'b', ' ', ' ', 'd', ' ', ' ', ' ',
'e', 'f', ' ' };
for (int i = 0; i < ch.length; i++) {
if (ch[i] == ' ') {
boolean swaped = false;
for (int j = i + 1; j < ch.length; j++) {
if (ch[j] != ' ') {
char temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
swaped = true;
break;
}
}
if (!swaped) {
break;
}
}
}

- krishna August 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
package my.practice.ds.puzzles;

import java.util.Arrays;

public class NextNonSpaceChar {

public static void main(String[] args) {
char ch[] = new char[] { 'a', 'a', 'b', ' ', ' ', 'd', ' ', ' ', ' ',
'e', 'f', ' ' };
for (int i = 0; i < ch.length; i++) {
if (ch[i] == ' ') {
boolean swaped = false;
for (int j = i + 1; j < ch.length; j++) {
if (ch[j] != ' ') {
char temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
swaped = true;
break;
}
}
if (!swaped) {
break;
}
}
}
System.out.println(Arrays.toString(ch));
}
}
}

- Anonymous August 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char[] arr = {a, a,b, , , c};
     String word = arr.toString().replace(",", "" ).replace(" ", "");

- timepass September 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

thanks all.. I could clear all the rounds.. had almost 7 technical rounds..can someone tell me how good it is to join? scope? just asking

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

start from the end of the string.
In this way we can eliminate the need for shifting the characters

- DashDash October 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

while((c = getchar()) != EOF)
if(c != ' ')
putchar(c)

- Anonymous October 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sgsdgds

- argsdgs October 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use regex.. [A-Za-z] or \w..

- Abhishek D. November 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void removeSpaces(char str[],int size)
{
    int i=0,j = size-1;
    char temp;
    while(i<j)
    {
        while(str[i] != ' ')
        {
            i++;
        }
        while(str[j] == ' ' )
        {
            j--;
        }

        if(i<j)
        {
            temp = str[i];
            str[i] = str[j];
            str[j] = temp;
        }
    }
    str[i+1] = "\0";
}

- r.sachdeva9355 December 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why not use divide and conquer? I know it would be the same time complexity with linear way. But it is an interview.
Come on bro!

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

import java.util.*;

public class RemoveSpaces {
	public static void main(String[] args) {
		char[] myChars = {'a', 'a', 'b', ' ', ' ', 'c', ' ', ' ', 'd'};
		String myString = new String(myChars);
		char[] charArrayNoSpaces = myString.replaceAll(" ", "").toCharArray();
	}
}

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

In C# we can use this:

input = new String(input.ToString().Where(Char.IsLetter).ToArray());

Console.Write(input);

- smartaquarius September 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Remember to use namespace

using System.Linq;

- smartaquarius September 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

java code

public class OnlyAlphabetsString {

	
	public static void main(String[] args) {
		
		String str= "a,a,b, , , c, , ,d,e";
		String delim= "[ ,]";
		String[] atr= str.split(delim);
		String output =atr[0];
		for(int i=1;i<atr.length;i++)
		{
			output+=atr[i];
		}
		System.out.println(output);
	}
}

- codingAddicted July 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