Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

1. Reverse the entire string once
2. Now reverse each word is the resultant string

- Sani October 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

static String reverseWords(String string){
		char array[] = reverseArray(string.toCharArray(),0,string.length());
		System.out.println(new String(array));
		int start = 0;
		for (int i = 0; i <= array.length; i++) {
			if(i == array.length || array[i] == ' ' ){
				array = reverseArray(array,start, i-start);
				start = i+1;
			}
		}
		return new String(array);
	}
	
	static char[] reverseArray(char array[],int start,int length){
		for (int i = 0; i < length/2; i++) {
			char tmp = array[i+start];
			array[i+start] = array[start+length-1-i];
			array[start+length-1-i] = tmp;
		}
		return array;
	}

- nmc January 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void reverse(char* pstr) {
char* start = pstr;

if (pstr == NULL)
return;

while (*pstr && *pstr != ' ')
pstr++;

if (*pstr == ' ') {
*pstr = '\0';
pstr++;
}
while (*pstr && *pstr == ' ')
pstr++;

if (*pstr == '\0'){
printf("%s ",start);
} else {
reverse(pstr);
printf("%s ",start);
}
}
int main()
{
char* str = "This is Java";
int l = strlen(str);

char* ptrf = (char*)calloc(l+1,1);
memcpy(ptrf,str,l);
printf("%s\n",ptrf);
reverse(ptrf);
printf("\b\n");

free(ptrf);
ptrf = NULL;
getchar();
}

- Chola Bhatura November 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

split the string by space and store in a list
print the list in reverse order and add space

- deepak November 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Reverse_String
{
public static void main(String arg[])
{
String data="This is java";
StringBuffer res=new StringBuffer();
char data_array[]=data.toCharArray();
System.out.println(data.substring(8,11));

int i=data.length()-1;
int d=0;
while(i>=0)
{
d=1;
while(data.charAt(i)!=' ' && i>0)
{
d=d+1;
i=i-1;
}
if(i==0)
res.append(data.substring(i,i+d).trim());
else
res.append(data.substring(i,i+d).trim() + " ");
i=i-1;
}
System.out.println(res.toString());

}


}

- durai November 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey9696" class="run-this">public class Reverse_String
{
public static void main(String arg[])
{
String data="This is java";
StringBuffer res=new StringBuffer();
char data_array[]=data.toCharArray();
System.out.println(data.substring(8,11));

int i=data.length()-1;
int d=0;
while(i>=0)
{
d=1;
while(data.charAt(i)!=' ' && i>0)
{
d=d+1;
i=i-1;
}
if(i==0)
res.append(data.substring(i,i+d).trim());
else
res.append(data.substring(i,i+d).trim() + " ");
i=i-1;
}
System.out.println(res.toString());

}


}
</pre><pre title="CodeMonkey9696" input="yes">
input:this is java
output:java is this</pre>

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

How about this.????

public class ReverseString {
static String reverse(String str){
str = new StringBuffer(str).reverse().toString();
StringBuffer res = new StringBuffer();
for(int i=0;i<str.length();){
StringBuffer temp = new StringBuffer();
while(i<str.length() && str.charAt(i)!= ' '){
temp.append(str.charAt(i));
i++;
}
res.append(temp.reverse());
while(i<str.length() && str.charAt(i) == ' ')
i++;
if(i<str.length())
res.append(' ');
}
return res.toString();
}
public static void main(String []args){
System.out.println(reverse("This is Java"));
}
}

- Vallabh November 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why can we not push the whole string in a stack and pop it out ??

- fchristopher239 November 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void  reverseString(String srt, int idx)
    {
    
        int endIdx = idx;
        while(endIdx < srt.length() && srt.charAt(endIdx) != ' '  )
              endIdx++;
        int passOnIdx = endIdx;
         while(passOnIdx < srt.length() && srt.charAt(passOnIdx) == ' ' )
              passOnIdx++;
         if(passOnIdx < srt.length())
         reverseString(srt,passOnIdx);
            
         System.out.print(srt.substring(idx, endIdx)+" ");   
        
    
    }

- pks December 10, 2011 | 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