Amazon Interview Question for Quality Assurance Engineers


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




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

1.Make a function to reverse the string passed:: String Reverse(String s){}
2.Reverse whole string.:: s=Reverse(s);
3.Split The String by space. a[] = s.Split(" ");
3.For each word in a, Reverse and print i.e:: a[i] = Reverse(a[i]) then print a[i].

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

No need to reverse the string

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

I am not sure why this is downvoted, but this is the most efficient solution for the problem

- this is the most efficient solution November 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

yes this is best solution for this problem......

- dhamu.31954 November 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

Anon used SPLIT which means that this functionality is supported by the language he was using (ex- Java). Therefore, if SPLIT is available just use it to obtain the array with different words and print the array in reverse.
If,however, the function SPLIT is not defined for a language than this might be the optimum solution, but that isnt the case here since point 3 clearly uses SPLIT.

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

we can create our own split easily
essentially brute force split is as good as language library split

- Anonymous November 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

If you are using a split, there is no point in reversing the string.
Just print the array obtained in reverse.

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

1. Split the string by space String arr[] = str.split(" ");
2. for (i=arr.length - 1; i>=0; i--)   System.out.print(arr[i] + " ");

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

My solution

String [] lSplitted = s.split(" ");
		for(int i = lSplitted.length - 1; i >= 0; i-- ) {
			System.out.print(lSplitted[i] + (i>0?" ":""));

}

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

Hi. Here is O(n) time complexity and O(1) memory solution that is readable and small, using C++.

void reverse_print(string const &s)
{
    copy(s.rbegin(), s.rend(), ostream_iterator<char>(cout, ""));
}

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

public String reverse(String ss){
if(ss.length()==0)return null;
else if(ss.length()==1)return ss;
else return ss.substring(ss.length()-1)+reverse(ss.substring(0, ss.length()-1));

}

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

why can't this be a s simple as

split with space
push all in stack
pop all

- Anonymous November 24, 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

private static String revereseWords(String inputString,char spaceChar){
		char[] charArray = inputString.toCharArray();
		int totalLength = charArray.length;
		char[] destArray = new char[totalLength];
		int spaceCount = 0;
		for (int i = totalLength , length = 0; i > 0; i--,length++) {
			if (charArray[i-1] != spaceChar){
				spaceCount ++;
			}else{
				System.arraycopy(charArray, i, destArray, length-spaceCount, spaceCount);
				destArray[length] = ' ';
				spaceCount = 0;
			}
		}
		if (spaceCount > 0){
			System.arraycopy(charArray, 0, destArray, totalLength-spaceCount, spaceCount);
		}
		return String.valueOf(destArray);
	}

My Solution without Split to avoid regex cost
System.out.println(revereseWords("This is test",' '));

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

Without use of any array split....

static void ReverseString(String ReverseString)
        {
            int endPrint = ReverseString.Length-1;

            for (int i = ReverseString.Length-1; i >= 0; i--)
            {
                if (ReverseString[i] == ' ' || i == 0)
                {
                    for (int j = i; j <= endPrint; j++)
                    {
                        Console.Write(ReverseString[j]);
                    }
                    endPrint = i;
                    Console.Write(" ");
                }
                
            }
        }

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

Ruby Code without using built in function-

data = String.new("This is Test")
l=data.length
p=0
a = Array.new
i=l-1
while i>=0 do
 a[p]= data[i]
 p=p+1
 i=i-1
end 
arr= a.inject(:+)
puts arr

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

-----Ruby Code------
str=String.new
n=String.new
str='This is a new year'
l=str.length
puts l
len=l-1
for i in 0...l
n[i]=str[len]
len=len-1
end
print n

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

Heres the solution in C without using stack

#include<stdio.h>

int main(){
	char string[] = " Hello i am shashank and how are you man!!";
	int i=0;
	int j=0;
	while(string[i] != '\0')
		i++;
	i--;
	while(i>=0){
		if(string[i] == ' ' || i==0){
			j=i+1;
			while(string[j] != ' ' && string[j] != '\0')
				printf("%c",string[j++]);
			printf(" ");
	  }
	  i--;
	}
}

- Shashank SIngh November 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here my C++ version:

string get_words_reverse(string &s) {
int j = s.size() - 1;

for(int i = 0; i < s.size()/2.0; i++) {
swap(s[i], s[j]);
j--;
}

for(int i = 0; i <= s.size(); i++) {
if(s[i] == ' ' || i == s.size()) {
int end = i - 1;
int len = end - start;

for(int j = 0; j <= len/2.0; j++) {
swap(s[start], s[end]);
start++;
end--;
}

start = i + 1;
}
}

return s;
}

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

>>> a = "This is a test"
>>> a[-1::-1]
'tset a si sihT'

- python code December 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void check()
{
index=0;
for(int i=0;i<input.length();i++)
{
if(i<(input.length()-1))
{
index=input.indexOf(" ");
if(index!=-1)
{
output= input.substring(0,index)+" "+output;
input=input.substring(index+1);
i=0;
}

}

else
{
output= input.substring(0)+" "+output;

}
}

System.out.println(output);
}

- Ankur Joshi December 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public String reverseString(String s) {
        if (s == null || s.length() <= 1) {
            return s;
        } else {
            return reverseString(s.substring(1)) + s.charAt(0);
        }
    }

- chris.longo December 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Stack;

public class PrintLineInReverse {

	public static String reverseString(String str){
		Stack<String> sk = new Stack<String>();
		StringBuilder sb = new StringBuilder();
		int lasti=0; boolean flag=false;
		String st;
		for(int i=0;i<str.length();i++){
			if(str.charAt(i)==' '){
				st = str.substring(lasti, i);
				sk.push(st);
				flag = true;
				continue;
			}
			if(flag){
				flag = false;
				lasti = i;
			}
		}
		st = str.substring(lasti, str.length());
		sk.push(st);
		while(!sk.isEmpty()){
			sb.append(sk.pop()+" ");
		}
		return sb.toString();
	}
	public static void main(String[] args) {
		String str = "This is test sdfsd";
		System.out.println(reverseString(str));
	}

}

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

String sentence = "This is test";
		String[] words = sentence.split(" ");
		for(int i = words.length-1 ; i >=0 ; i-- )
		{
			System.out.print(words[i]);
			if(i > 0)
				System.out.print(" ");
		}

	}

- NachiketN.89 January 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the C++ code:
int main() {

	string cszStr = "I am tester trying for new opportunities";

	int len = cszStr.length();

	for (int i=0,x=0; i<= len; i++, x++)
	{
		if (32 == (int)cszStr[len-i] || i == len)
		{
			for (int y = len-i; y <= ((len-i)+x); y++)
				cout << cszStr[y];
			x = 0;
		}
	}

	return 0;
}

- dsr January 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
public class ReverseStringByWords{

     public static void main(String []args){
         String str = "this is test";
         String[] arr = str.split(" ");
         
         for(int i=2;i>=0;i--){
             System.out.println(arr[i]);
         }
     }
}

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

String test= "This is test";
		String [] reverseTest= test.split(" ");
		for (int i= reverseTest.length-1; i>=0; i--){
			System.out.println(reverseTest[i]);

}

- engsin June 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package programs;

public class StringSplit {

public static void main(String[] args)
{
String str="This is Test";
String[] arr=str.split(" " );//splits the array by space
System.out.println(arr.length);
System.out.println("" +str);
for(int i=arr.length-1;i>=0;i--)
{
System.out.print(" "+arr[i]);

}
}
}

- Anonymous January 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package programs;

public class StringSplit {

public static void main(String[] args)
{
String str="This is Test";
String[] arr=str.split(" " );//splits the array by space
System.out.println(arr.length);
System.out.println("" +str);
for(int i=arr.length-1;i>=0;i--)
{
System.out.print(" "+arr[i]);

}
}
}

- Anonymous January 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

This is my C++ version, O(n) complexity and O(1) space (in-place). First reverses the entire string. Then reverse each word separated by one or more spaces.

#include <iostream>

void my_reverse(std::string& str, size_t begin, size_t end)
{
    while (begin < end)
        std::swap(str[begin++], str[end--]);
}

std::string reverse_words(std::string& phrase)
{
    // Reverse the entire string
	my_reverse(phrase, 0, phrase.size() - 1);
    
    // Reverse each word
    size_t begin_pos = 0;
    while (begin_pos != std::string::npos) {
        size_t end_pos = phrase.find(' ', begin_pos);
        if (end_pos == std::string::npos)
            end_pos = phrase.length();
        my_reverse(phrase, begin_pos, end_pos - 1);
        begin_pos = phrase.find_first_not_of(' ', end_pos + 1);
    }
    return phrase;
}

int main() {
	std::string str = "This is a test";
	std::cout << reverse_words(str) << std::endl;
	return 0;
}

- Diego Giagio November 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

line = 'This is test'
print line.split()[::-1]

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

why no love?

- Anonymous November 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

this is my CPP code working fine with few test.

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

string  stringRev(string & Str,int len)
{
 string arr[len];
 int i=0,j=0;
 int count =0;
          while(Str[i]!='\0') 
          {
                              
              if(Str[i]==' ')
              {                
                 arr[j]= Str.substr(count,i-count);
                 cout<<Str.substr(count,i-count)<<endl;
                 j++;
                           i++;
                 count=i;
              }
              else
              {
                  i++;
              }
                           
          }
          arr[j]=  Str.substr(count,i-count);   
          cout<<Str.substr(count,i-count)<<endl;
          string strnew;
          for(int k=len-1;k>=0;k--)
          {
                  strnew.append(arr[k]);
                  strnew.append(" ");                 
          } 
          cout<<strnew<<endl;
 return strnew;
}
int no_of_space(string &str)
{
 int len=0;
 int i=0;
 while(str[i]!= '\0')
 {
  if (str[i]==' ')
  {
                len++;
  }
  i++;
 }   
 len++;
 cout<<"Length :"<<len<<endl;
 return len;
}

int main(int argc, char *argv[])
{
    string str="Priyanshu is wrong what about this question";
    int len = no_of_space(str);
    string rtn = stringRev(str,len);
    cout<<rtn<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

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

why this is down voted this function will work for all the string input even for multiple spaces..

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

reverse(char *p,stack &s)
{
   if(*(p + 1) != '\0')
   {
     reverse(++p);
   }
   
   if(*p != ' ')
   {
     s.push(p);
   }
   else
   {
     s.print(); // This pops and prints all the chars pushed so far
   }   
}

- Shiva November 23, 2013 | 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