Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

I will assume the problem is to make an in-place modification.

void trimWhiteSpaces(char *s) {
   
    char *write = s; // If we modify the string, we use this pointer
    char *read = s; // we read using this one.
    bool nullTerminate = true;
    while (*read != '\0') {
       assert(write <= read);
       bool foundWhite = false;
       // Skip over white space.
        while (*read == ' ' ) {
            read++;
            foundWhite = true;
        }
    
        // Write space
        if (foundWhite) {*write = ' '; write++;}
    
        // Write the character read.
       *write = *read; 
       // It was NULL
       if (*read == '\0') {
           nullTerminate = false;
           break;
       }
       write++;
       read++;
    }
    
    if (nullTerminate) {
        *write = '\0';
    }
}

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

public static String removeWhenOccurMoreThanOne(String target){
char [] ch=target.toCharArray();
int end=0;
boolean bSwitch=true;
for (int i=0;i<target.length();++i){
if (ch[i]!=' '){
ch[end]=ch[i];
bSwitch=true;
end++;
}else if (ch[i]==' '&&bSwitch){
end++;
bSwitch=false;

}
}

return new String(ch,0,end);
}

- Scott May 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class TrimSpaces {

public static void main(String str[]){

String stri = "lohith is a developer";
System.out.println(stri.length());
char array[] = stri.toCharArray();



int index=-1,end=-1;

for(int i=0;i<array.length;i++){

if(array[i]==' '){
if(index==-1){
index=i;
}else{
end=i;
}
}else{
if(index !=-1 && end !=-1 ){

shiftStringAcrossSpaceFrom(index,end,array);
System.out.println(index + " index & end ="+end+" array="+new String(array));


i=index+1;
index=-1;end=-1;
}
}


}








}

private static void shiftStringAcrossSpaceFrom(int index, int endIndex,
char[] strichar) {

int j=0;
for(int i=endIndex;i<strichar.length;i++,j++)
strichar[index+j] = strichar[i];

int skipped =endIndex-index;

strichar[strichar.length - skipped] = '\0';
for(int i=strichar.length - skipped ; i <strichar.length ;i++){
strichar[i] = '\0';
}


}


}

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

void main()
{
char str[25];
int i,j,k;
gets(str);
for (i=0;str[i]!='\0';i++)
{
if(str[i]==' '&&str[i+1]==' ')
{

j=i+2;
while(str[j]==' ')
j++;
for(k=i;str[j]!='\0';k++,j++)
{
str[k]=str[j];
}
str[k]='\0';
i--;
}

}

puts(str);


}

- CC++ June 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class RemoveSpaces
{
	public static void RemoveSpaces(String ip)
	{
		String output = "";
		int i=0;
		while(i<ip.length())
		{
			if(ip.charAt(i)!= ' ')
			{
				output+=ip.charAt(i);
			}
			else
			{
				if(ip.charAt(i+1)!=' ')
					output+=ip.charAt(i);
			}
			i++;
		}
		System.out.println(output);
	}
	public static void main(String args[])
	{
		String input = "I      am                       Awe  some";
		RemoveSpaces(input);
	}
}

Why are everyone moving the spaces to the end of the string?
Instead of using a Char array, we can directly use a String - dynamic data structure.
Can someone correct me if I am wrong.

- Someone December 12, 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