Bloomberg LP Amazon Interview Question for Financial Software Developers Software Engineer / Developers






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

public String removeMultipleSpaces(String word){
        if(word == null){
            return word;
        }
        int pivot1 = 0;
        int pivot2 = 0;
        char[]  words = word.toCharArray();
        int length = words.length-1;
        while (pivot1 <= length){
            while (pivot1 <= length && (words[pivot1]) != ' '){
                words[pivot2] = words[pivot1];
                pivot2++;
                pivot1++;
            }
            if(words[pivot1] == ' '){
                words[pivot2] = words[pivot1];
                pivot1++;
                pivot2++;
            }
            while (pivot1 <= length && (words[pivot1] == ' ')){
                pivot1++;
            }
        }
        String result =  String.copyValueOf(words);
        return result.substring(0,pivot2);
    }

- m3th0d.itbhu April 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Replace and? Scoot over the other chars? This requires careful coding and it would be easy to go wrong. I would not be surprised if the code I attempt below is wrong.

Idea is, basically go through the string, and when we hit a space, go in a loop to ignore the next space chars. We maintain two pointers, one which goes through given string, and the other which corresponds to what the final string would be.

void trimspaces(char *s) {
    
    char *curRead;
    char *curCopy;
    int foundSpace = 0;
    
    if (s == NULL || *s == NULL) { return;}
    
    curRead = s;
    curCopy  = s;
    while (*curRead != NULL){
        
        // The previous character was a space.
        // Go into loop ignoring the spaces.
        if (foundSpace) {
            while(*curRead == ' ') {
                curRead++;
            }
            // Reached the end of the string.
            if (*curRead == NULL) {
                break;
            }
            foundSpace = 0;
        }

        if (*curRead == ' ' && !foundSpace) {
            foundSpace = 1;
        }
        
        *curCopy = *curRead;
        curCopy++;
        curRead++;
    }
    // Copy over the NULL byte.
    *curCopy = NULL;
}

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

Use Run length coding.

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

What?

Isn't this the same as: http://www.careercup.com/question?id=88827

?

- T March 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

C:

void trimspaces(char *s){
  char *a=s;
  int i=0;

  while(s[i++]=*(a++))              /* copy character */
    while(*a==' ' && *(a+1)==' ')   /* two spaces detected */
      ++a;                          /* skip a space */
}

C++:

void trimspaces(string &s){
  s.resize(unique(s.begin(), s.end(), dupeSpaces)-s.begin());
}

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

bool dupeSpaces(char i, char j){
  return (i==j && i==' ');
}

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

int
trim_spaces(char *s) 
{
        char *a = s;
        if (s == NULL)
                return (FAILURE);
        while ( (*s = *a) != '\0') {
                while ( *a == ' ' && *(a + 1) == ' ')
                        a++;
                a++;
                s++;
        }   
        return (SUCCESS);
}

- Balaji April 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A generic solution to replace anything
strReplaceAllOccurance(std::string strBaseString,std::string strToFind,std::string strToReplace)
{
int intDotLocation;

//Search the complete string for strToFind Recursively and replace it with required string
while((intDotLocation = strBaseString.find_first_of(strToFind)) != -1)
strBaseString.replace(intDotLocation,1,strToReplace);

return strBaseString;
}

- Varun April 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String st=" aaaaa bbbbbbbb cccccccc dddddddddddddddd";
String arr[]=st.split(" ");
String str="";
for(int i=0;i<arr.length;i++){
if(arr[i].trim().length()!=0){
str=str+arr[i]+" ";
System.out.println(arr[i]);
}
}
System.out.println(str);
}

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

add more space in between and try in previous code(Anonymous on April 08, 2009

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

s/ +/ /g

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

protected String trimSpaces(String value){
    StringTokenizer tokenize = new StringTokenizer (value);
    StringBuilder returnValue = null;
    while( tokenize.hasNext()){
        returnValue.append(tokenize.next()+" ");
    }
    return returnValue.toString().trim();
}

- CyberPhoenix May 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Interviewers expect a code without stringtokenizers. Also when a null value is given, the return value here will be "null". Finally, it is tokenize.nextToken().

- G May 13, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void PruneSpaces(char* szStr)
{
size_t len = strlen(szStr);
size_t writePos = 0;
for (size_t i=1;i<=len;i++)
{
if ((szStr[i] != ' ') || (szStr[writePos] != ' '))
{
szStr[++writePos]=szStr[i];
}
}
}

- KK May 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void PruneSpaces(char* szStr)
{
    size_t len = strlen(szStr);
    size_t writePos = 0;
    for (size_t i=1;i<=len;i++)
    {
        if ((szStr[i] != ' ') || (szStr[writePos] != ' '))
        {
           szStr[++writePos]=szStr[i];
        }
    }
}

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

Given the input "Hello how are you"
The output is "ello how are you"

The code begins iteration from 1st character i.e. i = 1 and 0, any reason for doing that. The output incorrect.

- Vincent July 18, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

string PruneSpace(string s)
{
	if(s== null || s.Length == 0)
		return s;
	StringBuilder sb = new StringBuilder(s);
	int pos = 0;
	int spaceIndex;
	while (pos < sb.Length)
	{
		spaceIndex = sb.ToString().IndexOf(' ', pos);
		pos = spaceIndex + 1;
		while (sb[pos] == ' ')
			pos++;
		sb.Replace(' ', '', spaceIndex, pos - spaceIndex);
		pos = spaceIndex + 1;
	}
	return sb.ToString();
}

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

why not just use the delete method

- jr June 07, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <string.h>
#include <malloc.h>
void RemoveSpace( char *str)
{
char *tmp = (char*)malloc(sizeof(str));
strcpy(tmp, str);
char *s1;
s1 = strtok(tmp," ");
while(s1 != NULL)
{
printf("%s", s1);
s1 = strtok(NULL, " ");
}

}

- Alok October 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

good code man!!

- Anonymous June 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

good code man!!

- Anonymous June 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String trimSpaces(String str)
    {
    	StringBuilder sb=new StringBuilder();
    	for(int i=0;i<str.length();i++)
    	{
    		if(str.charAt(i)==' ')
    			sb.append(str.charAt(i));
    		while(str.charAt(i)==' ') 
    			i++;
    		sb.append(str.charAt(i));
    	}
    	return sb.toString();
    }

- Rakesh Kumar November 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReplaceMultipleSpaces {

	public static void main(String args[]) {
		String str = "Hi    This  is   Code for     replacing multiple    Spaces     ";
		String str2 = "";
		int cnt = 0;
		System.out.println(str);

		char[] strArr = str.toCharArray();

		for (int i = 0; i < strArr.length; i++) {
			if (strArr[i] == ' ')
				cnt++;
			else
				cnt = 0;
			if (cnt > 1)
				continue;
			str2 = str2 + strArr[i];

		}
		System.out.println(str2);
	}

}

- sl714 August 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package string;

public class ReplaceMultipleStrings {
	
	
	public static void main(String[] args) {
		
		
		System.out.println(replaceMultSpace("Suha  a   s   abc  wedd"));
	}
	static String replaceMultSpace(String inp) {
		
		if(inp == null || inp.isEmpty()) 
			return null;
		
		int len = inp.length();
		
		for(int i=0;i<len;i++) {
			
			if(inp.charAt(i)==' ') {
				int spCount = 1;
				for(int j=i+1;j<len;j++) {
					
					if(inp.charAt(j)==' '){
						spCount++;
					}
					else
						break;
				}
				
				if(spCount>1) {
					
					inp = inp.substring(0,i)+" "+inp.substring(i+spCount,len);
					len = inp.length();
				}
			}
		}
		
		return inp;
	
	

	}
}

- shukad333 August 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Space Complexity O(1) and time complexity O(n)

void ReplaceMultipleSpaces(char* str)
{
int nCount=0;
int nPrev=0;
while(str[nCount]!=NULL && str[nCount+1]!=NULL)
{
if(str[nCount]==' '&& str[nCount+1]==' ')
nPrev++;
else
str[nCount+1-nPrev]=str[nCount+1];
nCount++;
}
str[nCount+1-nPrev]='\0';
}

- Pramod September 24, 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
		removespaces("This    is  an        example string");
	}
	public static void removespaces(String str)
	{
		char t[]=str.toCharArray();
		int spaceindex=0;
		int count=0;
		for(int i=0;i<t.length;i++)
		{
			if(t[i]==' ')
			{
				count++;
				continue;
			}
			else
			{
				if(count>0)
				{
					t[spaceindex]=' ';
					spaceindex++;
					count=0;
					
				}
				t[spaceindex]=t[i];
				spaceindex++;
				
			}
		}
		String temp=new String(t);
		
		System.out.println(temp.substring(0,spaceindex));
	}

- Anonymous April 25, 2015 | 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
		removespaces("This    is  an        example string");
	}
	public static void removespaces(String str)
	{
		char t[]=str.toCharArray();
		int spaceindex=0;
		int count=0;
		for(int i=0;i<t.length;i++)
		{
			if(t[i]==' ')
			{
				count++;
				continue;
			}
			else
			{
				if(count>0)
				{
					t[spaceindex]=' ';
					spaceindex++;
					count=0;
					
				}
				t[spaceindex]=t[i];
				spaceindex++;
				
			}
		}
		String temp=new String(t);
		
		System.out.println(temp.substring(0,spaceindex));
	}

- Anonymous April 25, 2015 | 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