Amazon Interview Question for Quality Assurance Engineers


Country: United States
Interview Type: Phone Interview




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

swap first with last
....
then?

- S O U N D W A V E October 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Swap first with last character.

if(str == null){
 return;
}
i =0;
j = str.length() -1;
while(i< j){
     swap(str.charAt(i), str.charAt(j));
      i++;
      j--;
}

- Ajeet October 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class rstr {

	
	static String rString(String str){
	
		StringBuilder sb = new StringBuilder();
		
		for (int i = str.length()-1; i >=0; i--) {
			sb.append(str.charAt(i));
			
		}
		
		
		
		return sb.toString();
			
		
	}
	
	public static void main(String[] args) {
		
		
		System.out.println(rString("Trying is Good"));

	}

}

- Amit Singh October 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

public class rstr {

static char[] rString(String str){

char[] rstr = new char[str.length()];
System.out.println("str.length(): " + str.length());
for (int i = str.length()-1; i >= str.length()/2; i--) {
if(str.length()-1-i >= 0) {
rstr[str.length()-1-i] = str.charAt(i);
rstr[i] = str.charAt(str.length()-1-i);
}
}

return rstr;

}

public static void main(String[] args) {
System.out.println(rString("Trying is Good"));
}

}

- Suresh Tirumalasetti October 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int i = 0;
int j = str.length() - 1;
char[] strArr = str.toCharArray();
while(i != j) {
char c = str.charAt(i);
char l = str.charAt(j);
strArr[i] = l;
strArr[j] = c;
i++;
j--;
}

- Anonymous October 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

QA will make that throw an "Array Index Bounds Exception"

Your second line, LENGTH -1, <-- this "can" be 0 - 1 = -1

And no need to charAt insidethe loop. And you need to copy back to str.
Your method should be something like this:

char[] A = str.toCharArray();
for(int i = 0, j = str.length() - 1; i < j ; i++, j-- )
	swap( A[i] , A[j] );
str=String.valueOf(A);

- S O U N D W A V E October 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

or if you insist

char[] A = str.toCharArray();
for(int i = 0, j = str.length() - 1; i < j ; i++, j-- )
{
	A[i] = A[j];
	A[j] = str.charAt(i);
}	
str=String.valueOf(A);

No need for your l and c.

- S O U N D W A V E October 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

The most common thing .. it will not work for string with even size .. like .. "ABCD". Pointers will pass each other and one time they will through ArrayIndexOutOfBound something exception.

- Ajeet October 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

+1 @Ajeet.

And QA will always test empty string as first test case, so any developer should mentally check that too.

- S O U N D W A V E October 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, my reply was just add on to your reply ... :)

- Ajeet October 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

For fun only
One liner (C++ maybe) for maximum ugliness:

for(int i=0; int j=strlen(s)-1; i < j; s[i++]^=s[j--]^=s[i]^=s[j] )  ;

Wonder if that works....

- S O U N D W A V E October 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

for(int i=0, j=strlen(s)-1; i < j; s[i++]^=s[j--]^=s[i]^=s[j] )  ;

That might.

- S O U N D W A V E October 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void ReverseStr(char *s)
{
  try
  {  
     if(!*s) throw "\nNull String\n";
  }catch(const char *p)
    {
       puts(p);
       return;
    }
    int begin=0;
    int end=strlen(s)-1;
    while(begin<end)
    {
       s[begin]^=s[end];  
       s[end]^=s[begin];
       s[begin]^=s[end];
       begin++;
       end--;
    }
}

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

def reverse(str):
	reverseStr = list()
	for char in str:
		reverseStr.push(char)
	return "".join(reverseStr)

# or
def reverse2(str):
	return str[::-1]

- rohan.rapidwolverine October 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseString {

public static String reverse(int pos, char[] stringtoreverse, String reversed){
if(pos==0)
return reversed + stringtoreverse[0];
else
return reverse(pos-1, stringtoreverse, reversed + stringtoreverse[pos]);
}
}

- goantech October 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void reverStr(String str)
	{
		char[] charArr = str.toCharArray();
		for(int i=(charArr.length-1);;i--)
		{
			System.out.print(charArr[i]);
			if(i==0) break;
		}
	}

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

public class Reverse {

static String s = "abcdefg";

static String reverse(){
char a[] = new char[s.length()];
a = s.toCharArray();

int i = 0, j = s.length()-1;
while(i < j){
char c = a[i];
a[i] = a[j];
a[j] = c;
i++;j--;
}

s = new String(a);

return s;
}

public static void main(String args[]){
if(s.length()==0)
System.out.println("Empty String");
else
System.out.println(reverse());
}

}

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

public static void main(String args[])
{
String s;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter A String");
s = br.readLine();
StringBuffer s1 = new StringBuffer(s);
s1.reverse();
System.out.print("Reversed String is "+s1);
}

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

I'm new at this. Is using standard c/C++ libraries allowed for things like that?

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

int main()
{
	string s = "Hello World!";
	cout << s << endl;

	//reverse string in-place
	std::reverse(s.begin(), s.end());
	cout << s << endl;

	//reverse string
	s = string(s.rbegin(), s.rend());
	cout << s << endl;

	cin.ignore();
	return 0;
}

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

In place. best i ever saw before to do string reverse

string revers(string str){
	if(str.length()<=1)
		return str;
	return revers(str.substr(1,str.length()))+ str.at(0);
}

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

Ruby Code

data = String.new("Good Morning")
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

- Nitin N November 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

So here for reversing a string, we have very simple algo :-
Take the string and convert it into char array by using toCharArray() function and take a temp char var.
Now start a for loop which will run until half of the length of array.
In this loop just swap the values using temp var like first value of array store in temp, and last element of array move to first and temp move to last.
That all now print the array by using for loop.

Here is the code implementation for the same :

import java.util.*;
public class ReverseString{

     public static void main(String []args){
         String str = "this is me";
         char[] arr = str.toCharArray();
         int len = arr.length;
         for(int i=0;i<arr.length/2;i++){
             char temp = 0;
             temp = arr[i];
             arr[i] = arr[len-i-1];
             arr[len-i-1] = temp;
         }
         for(int i=0;i<arr.length;i++)
        System.out.println(arr[i]);
     }
}

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

Python code:

str="Email me when people comment"
print str[::-1]

- Mayank September 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String reverse= "reverse this string";
for (int i=reverse.length()-1;i>=0;i--){
reverse.charAt(i);
System.out.print(reverse.charAt(i));
}

- Alok July 26, 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