Amazon Interview Question for Testing / Quality Assurances






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

ex: 1020 -> 201 ?
or should the answer be 021, i.e. 21?

For second variant:

int reverse(int num)
{
    bool used[10] = {0,0,0,0,0,0,0,0,0,0};
    int rev = 0;
    while(num)
    {
        int digit = num%10;
        if (!used[digit])
        {
            rev = 10*rev+digit;
            used[digit] = true;
        }
        num /= 10;
    }
    return rev;
}

- strezh September 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Shouldn't the answer be 254???

- Anonymous September 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

254 also correct,write code

- Anonymous September 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In python, its easy
no=2452

''.join(list(set(str(no)[::-1])))

- Nish December 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int reverse(int num)
{ int n;
int rev = 0;
n = num;

while(n >0)
{
t = num%10;
rev = rev *10 + t;

n = n/10;
}

return rev;

}

- Anonymous September 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, forgot to check for repeating digits.

int reverse(int num)
{ int n;
int rev = 0;
int flag = 1;
n = num;

while(n >0)
{
t = num%10;
temp = rev;
while(temp >0)
{
if (t == temp%10)
{
flag=0;
break;
}
else
{
flag = 1;
temp = temp/10;
}
}

if(flag == 1)
rev = rev *10 + t;

n = n/10;
}

return rev;

}

- Anonymous September 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int Reverse(int num)
{
int mod=0, ret=0;
int arr[10]={0};
while(num)
{
mod=num%10;
if(arr[mod]==0)
{
arr[mod]=1;
ret=ret*10+mod;
}
num=num/10;
}
return ret;
}

The idea is to have an array of size 10 (having all digits from 0-9) whic keeps a note of whether an element is repeated or not.

- _pkm September 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nicely done!

- Metta September 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The code fail when the input is 2002, it display 20, correct should be 2.

- Anonymous September 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The code fail when the input is 2002, it display 20, correct should be 2.

- Anonymous September 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

int Reverse(int num)
{
int d = ceil(log(num)/log(10)); //log is base 10
int ret;
bool _pkm[10]={false,};
int i=0;
while(d > 0)
{
int msd = power(10, d);
if(!_pkm[msd])
{
ret = msd*power(10,i) + ret;
pkm[msd]=true;
}
num = num%power(10,d);
d--;
}
return ret;
}

- novino September 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int reverseNumber(int num, int r, HashSet digits){
			 if(num<=0){
				 System.out.println("reverse "+ r);
				 return r;
			 }
			 int lastDigit = num%10;
			 if(digits.add(lastDigit)){
				 r = r*10 +lastDigit;
			 }
			 
			 num = num/10;
			 reverseNumber(num, r,digits);
			 return r;
		 }

call as
reverseNumber(4324,0,new HashSet());

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

<pre lang="c++" line="1" title="CodeMonkey5950" class="run-this">#include<iostream>
using namespace std;
int main()
{
int num,rev=0,x=0;
bool numbrs[10]={0};
cout<<"Enter a number";
cin>>num;
while(num>0)
{
x=num%10;
if(numbrs[x]!=1)
{
rev=rev*10+x;
numbrs[x]=1;
}
num=num/10;
}
cout<<endl<<rev;
system("pause");
return 0;
}

</pre><pre title="CodeMonkey5950" input="yes">
</pre>

- Anonymous November 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think dis nw works..
any suggestions??

- shekhar November 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

$num=56439823; #say for example
@num=split("",$num);
$len=scalar(@num);
$i=$len-1;

while($i>=0){
$j=0;
while($j<=$len-1){
if($num[$i]==$num[$j]){unless ($i==$j){$num[$i]

=undef;}}
$j++;
}
$i--;
}


print reverse(@num);

- here is perl code: January 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using Java:

import java.util.*;
public class ReverseInteger {

public static void main(String[] args){
int i = 55151321;
int rev = 0;
List<Integer> tempList = new LinkedList<Integer>();
boolean foundDuplicate = false;
while (i>=1)
{
ListIterator<Integer> it = tempList.listIterator();
while(it.hasNext())
{
if(it.next() == i%10 )
{
System.out.println("Found duplicate - Ignoring:" + i%10);
foundDuplicate = true;
}
}
if(!foundDuplicate)
{
tempList.add(i%10);
rev = (rev*10)+(i%10);
}
foundDuplicate = false;
i=i/10;
}
System.out.println("The List "+ tempList);
System.out.println("rev without duplicate=" + rev );
}
}

- Milind April 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey47098" class="run-this">import java.util.*;
public class ReverseInteger {

public static void main(String[] args){
int i = 55151321;
int rev = 0;
List<Integer> tempList = new LinkedList<Integer>();
boolean foundDuplicate = false;
while (i>=1)
{
ListIterator<Integer> it = tempList.listIterator();
while(it.hasNext())
{
if(it.next() == i%10 )
{
System.out.println("Found duplicate - Ignoring:" + i%10);
foundDuplicate = true;
}
}
if(!foundDuplicate)
{
tempList.add(i%10);
rev = (rev*10)+(i%10);
}
foundDuplicate = false;
i=i/10;
}
System.out.println("The List "+ tempList);
System.out.println("rev without duplicate=" + rev );
}
}

</pre><pre title="CodeMonkey47098" input="yes">
</pre>

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

this is a standard question.
an idiot who interviewed me asked this one and when i answered this in a few minutes, he started giving me lectures from Gita, saying answers should be spontaneous and without preparation.

its true idiot. had your questions been spontaneous, my answers would have been.

- anon August 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL

- Anonymous October 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ROFL

- Anonymous June 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ROFL

- Anonymous June 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ROFL

- abc June 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ROFL

- abc June 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num,a[9],i,temp,rev=0;
for(i=0;i<9;i++)
a[i]=0;
printf("Enter num=");
scanf("%d",&num);
while(num>0)
{
temp=num%10;
if(a[temp]!=1)
{
rev=rev*10+temp;
a[temp]=1;
}
num=num/10;
}
printf("\n%d",rev);
getch();
}

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

I tried by executing above code, but they are not producing the desired output of "542" for the input of "2452". Here is my code:

#include <stdio.h>
main()
{
     int num = 0, dup, rev = 0;
     int i = 0, arr[10]= {0};
     printf ("\n Please enter a number: ");
     scanf ("%d", &num);
     dup = num;
     while (dup > 0) {
          i = dup%10;
          arr[i]++;
          dup = dup/10;
     }
     dup = num;
     while (dup > 0) {
          i = dup%10;
          if (arr[i] == 1) 
               rev = rev * 10 + i;
          else
               arr[i]--;
          dup = dup/10;
     }
     printf ("\n Reverse number is: %d \n", rev);
}

- Priyab February 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sdsd

- Manoj July 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int Reverse(int num){
int rev1,rev2;
int temp;
rev1=0;rev2=0; temp=0;
while(num>0){
temp=num%10;
if(!Used(rev2,temp)){
rev2=rev2*10+temp;
rev1=rev2;
num=num/10

}
else{
num=num/10;
}
}
}

int Used(int num,int temp){
int x=0;
while(num>0){
x=num%10;
if (x==temp){
return 1;
}
else{
num=num/10;

}
}

}

- Manoj July 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Python, set() doesn't keep the order

a='2452'
a=(a[::-1])
result = []
for i in a:
    if i not in result:
        result.append(i)
print(''.join(result))

- Jareen November 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.io.*;
import java.util.*;

public class revEliminate
{

public static void main(String[] args)
{
String s= "2452";
int a [] = new int[4];
int b=0;
int d =0;
int counter=0;

System.out.println("The original Array");
for(int i=0; i<s.length(); i++)
{
b = Character.getNumericValue(s.charAt(i));
a[i]= b;
System.out.println("\t"+a[i]);
}

int aLength=a.length;
int c[] = new int[aLength];
for(int j=0 ; j<aLength-1 ; j++)
{
for(int k=aLength-1; k>j ; k--)
{
if(a[j]!=a[k])
{
d=a[j];

}

}
c[j]=d;
counter=counter+1;
d=0;
}
System.out.println();
System.out.println("The Duplicates Eliminated Array");
for (int l=0; l<counter ; l++)
{
System.out.println("\t"+c[l]);
}
System.out.println();
System.out.println("Counter of duplicate eliminate array:"+counter);
int e [] = new int[counter];
int n=0;

for (int m=counter-1; m>=0; m--)
{
e[n]=c[m];
n++;

}
System.out.println();
System.out.println("The Reversed Array array:");
for (int index=0; index<counter ; index++)
{
System.out.println("\t"+e[index]);
}

}

}

- Anonymous May 27, 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