Amazon Interview Question for Quality Assurance Engineers


Team: Kindle
Country: India
Interview Type: In-Person




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

This can be done in two steps.
1. Reverse the entire string.
2. Reverse each word (separated by space).

Ex..
Input : "This is test"
After step 1: "tset si sihT"
After step 2: "test is This".

- Rahul November 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Or you can split the string at space and than print the array obtained in reverse.

- fReaK November 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

public static void printReverseWordOrder(String str)
{
	String[] words = str.split(“ ”);
	ArrayUtils.reverse(words);
	StringBuilder sb = new StringBuilder();

	for(String word: words)
		sb,append(word);
	System.out.println(sb.toString());
}

- zahidbuet106 December 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

do it one pass;
have 2 pointers, 1 points to the end of the last full word

start with lastPointer at end of input string

make currentpointer = lastPointer

loop decrement currentpointer until currentpointer = 0

if inputstring indexed by CurrentPointer = word break

print out everything between currentPointer and LastPointer
make lastpointer = current pointer
go back into loop

- Woody November 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class Reversestring {
public static void main(String[] args) {
String s ="Hello world how r u ?";
String[] a = s.split(" ");
System.out.println(a.length);
String p="";
for(int i=a.length-1;i>=0;i--)
{
p=p+a[i]+" ";
}
System.out.println(p);
}
}

- premkarthi20 December 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String str = "This is test";
		String[] strArray = str.split(" ");
		String newStr= "";
		for(String s:strArray){
			newStr = s + ("".equals(newStr)?newStr:" ") + newStr ;
		}
		System.out.println(newStr);

- Harshul Pandav November 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#!/usr/bin/python

# Print words of given string in reverse order: 
#    "This is test" 
#    Output: "test is This"

def reverse(input, start, end):
    if start < end - 1:
        input[start], input[end - 1] = input[end - 1], input[start]
        reverse(input, (start + 1), (end - 1))
    return input

string = 'This is test'
input = [ x for x in string.split() ]
print reverse(input, 0, len(input))

- James November 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Three solutions came to my mind.

1) Split the string and iterate over the array starting from the last position to the first one.

2) Iterate over the string from the last position and adding the chars into a Stack. Every time the iteration reaches a ' ', remove the elements from the stack and print.

3) Using no high level language methods or extra data structures. Similar to the first answer to this question. Using two indexes and iterating from the last position. Here is the code:

String s = "this is a test";
		
		int current, prev;
		current = prev = s.length()-1;
		
		while (current >= 0) {
			
			if (s.charAt(current) != ' ') {
				current--;
			} else {
				for (int i = current+1; i <= prev; i++)
					System.out.print(s.charAt(i));
				System.out.print(" ");
				
				prev = current-1;
				current--;
			}
			
		}
		
		for (int i = 0; i <= prev; i++) {
			System.out.print(s.charAt(i));
		}

- joaoarthurbm November 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think I have the best solution, please see the code below.

int main()
{
	char c[] = "this is test";
	int i,j;
	for(i=strlen(c)-1;i>=0;i--) {
		if((c[i] == ' ') || (i == 0)) {
			j = (i == 0) ? i : i+1;
			for(;j<strlen(c) && (c[j] != ' ');j++)
				printf("%c",c[j]);
		printf(" ");
		}
	}
	return 0;
}

- agmegharaj November 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void reverseSentence(String insentence) {
		char[] sentence = insentence.toCharArray();
		int[] blankposlist = new int[100];

		displayArray(sentence);
		int k = 0;
		blankposlist[k++] = -1;

		// storing the position of blank space in the input string.
		for (int i = 0; i < sentence.length; i++) {
			if (sentence[i] == ' ') {
				blankposlist[k++] = i;
			}
		}
		blankposlist[k] = sentence.length;
		// reverse the input string
		reverseString(sentence, 0, sentence.length - 1);

		// Reverse each word
		for (int m = 0; m < sentence.length;) {
			int reverselen = blankposlist[k] - blankposlist[k - 1] - 1;
			reverseString(sentence, m, m + reverselen - 1);
			m = m + reverselen + 1;
			k--;
		}

		displayArray(sentence);
	}

- Rudra November 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

First reverse the total String and which will give entire reveresed String. from here we need to reverse each word endividually

package com.practise.amazon;

import java.util.StringTokenizer;

public class ReverseWords {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String original = "This is Test";
		System.out.println(original);
		System.out.println(reverseWords(original));
	}

	private static String reverseWords (String original) {
		String reversedString = reverseString(original);
		StringTokenizer st = new StringTokenizer(reversedString, " ");
		StringBuffer sb = new StringBuffer();
		while (st.hasMoreElements()) {
			sb.append(reverseString(st.nextToken()) + " ");
		}
		return sb.toString().substring(0, sb.length() - 1);
	}
	
	private static String reverseString (String original) {
		char[] c = original.toCharArray();
		int len = original.length() - 1;
		int l = 0;
		while (len > l) {
			char x = c [len];
			c[len] = c[l];
			c[l] = x;
			l++;
			len--;
		}
		return new String (c);
	}

}

- nishant November 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct stack{
    char **stk;
    int top;
    int stk_size;
}S_t;

S_t* init_stk(S_t *s, int stkSize, int dataLen)
{
    if( s == NULL ) {
        s = malloc( sizeof(S_t) );
        if( !s )
            return NULL;
    }

    s->stk  =   (char **) malloc( sizeof(char *) * stkSize );
    if( !s->stk )
        return NULL;


    int idx = 0;
    for ( ; idx < stkSize ;idx ++ ) {
        s->stk[idx] = malloc ( sizeof(char) * dataLen );
        if ( ! s->stk[idx] )
            return NULL;

        memset(s->stk[idx], 0x00, dataLen );
    }

    s->stk_size =   stkSize;
    s->top      =   -1;

    return s;
}


int push( S_t *s, char *str )
{
    if( s->top == s->stk_size-1)
        return -1;

    s->top  +=  1;
    strcpy( s->stk[s->top], str );
    return 0;
}

char *pop ( S_t *s)
{
    if( s->top == -1)
        return NULL;
    char *ptr = s->stk[s->top];
    s->top -= 1;

    return ptr;
}

int main ()
{

    S_t *s  =   NULL;
    s = init_stk(s, 100, 100);

    char buff[1024] = {0,};

    fgets( buff, 1024, stdin);
    int bLen    =   strlen(buff);
    buff[bLen-1]    =   '\0';
    char *token =  NULL;
    char *sptr  =  NULL;
    char *buf   =   buff;
    for ( ; ; buf   =   NULL )
    {
        token = strtok_r(buf," ",&sptr);
        if( token == NULL)
            break;
        push(s,token);
    }

    while ( token = pop(s) )
        printf("%s ", token);

    return 0;
}

- A C-Program using stack November 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseByString
{
public static void main( String[] args )
{
if(args.length < 1)
{
System.out.println("Syntax: ReverseByString <string>");
return;
}
System.out.println(args[0]);

reverseByString(args[0].toCharArray(), 0);
}

static void reverseByString(char[] arg, int index)
{
int n = index;
for(; n < arg.length; n++)
{
if(arg[n] == ' ')
{
reverseByString(arg, n+1);
break;
}
}
for(int i = index; i <n; i++)
{
System.out.print(arg[i]);
}
if(index != 0)
System.out.print(' ');
}
}

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

public class ReverseByString
{
public static void main( String[] args )
{
if(args.length < 1)
{
System.out.println("Syntax: ReverseByString <string>");
return;
}
System.out.println(args[0]);

reverseByString(args[0].toCharArray(), 0);
}

static void reverseByString(char[] arg, int index)
{
int n = index;
for(; n < arg.length; n++)
{
if(arg[n] == ' ')
{
reverseByString(arg, n+1);
break;
}
}
for(int i = index; i <n; i++)
{
System.out.print(arg[i]);
}
if(index != 0)
System.out.print(' ');
}
}

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

#include<iostream>
using namespace std;

string reverse(string str){
string revStr;
revStr.reserve(25);
cout<<str<<"\n";
int len = str.length()-1;
while(len >= 0){
int i = len;
while((len >= 0) && (str[len--] != ' '));
if(len > 0){
revStr.append(str.begin()+len+2 , str.begin()+i+1);
revStr = revStr + ' ';
}
else
revStr.append(str.begin()+len+1 , str.begin()+i+1);
}
return revStr;
}

int main(){
string str = "you are going to suffer!";
string rev = reverse(str);
cout<<rev<<"\n";
}

- Sunny Naval Singh December 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Approach :1 step1 : Reverse the whole Sentence
	// step 2: Reverse each word again
	public static String reverseSentence(String sentr) {
		int i = 0;
		int j = 0;
	
		String sent = String.valueOf(reverseWord(sentr.toCharArray(), i, sentr.length() - 1));
		System.out.println("Initial Reverse = " + sent);

		while ((j < sent.length())) {

			if (sent.toCharArray()[j] != ' ') {
				i = j;
				System.out.println("i=" + i);

				while (j < sent.length() && sent.toCharArray()[j] != ' ') {
					j++;
				}
				j--;
				System.out.println("j=" + j);
				sent = reverseWord(sent.toCharArray(), i, j);
			}
			j++;

		}
		String rStr = String.valueOf(sent.toCharArray());
		System.out.println(rStr);
		return rStr;
	}


	// Reverse Word
	public static String reverseWord(char[] str, int start, int end) {
		// Start swapping char
		char temp;
		while (start < end) {
			temp = str[start];
			str[start] = str[end];
			str[end] = temp;
			start++;
			end--;
		}
		return String.valueOf(str);
	}

- arianSonia August 07, 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