Amazon Interview Question for Interns


Country: India
Interview Type: Written Test




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

f(513) = 5 * f(99) + f(13) + 100 {Remainder: 13, 100 for 200 - 299}
f(279) = 2 * f(99) + f(79) + 79 + 1 {Remainder: 79, 1 for 200}

public static int count2sR(int n) {
// Base case
if (n == 0) return 0;

// 513 into 5 * 100 + 13. [Power = 100; First = 5; Remainder = 13]
int power = 1;
while (10 * power < n) power *= 10;
int first = n / power;
int remainder = n % power;
// Counts 2s from first digit
int nTwosFirst = 0;
if (first > 2) nTwosFirst += power;
else if (first == 2) nTwosFirst += remainder + 1;

// Count 2s from all other digits
int nTwosOther = first * count2sR(power - 1) + count2sR(remainder);

return nTwosFirst + nTwosOther;
}

- Adnan Ahmad August 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

It doesn't work. count2sR(22) returns 6 while it should be 5

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

Below is a recursive approach which will print the number of 2's from 0-n. Please execute the code and correct me if I am wrong

public class Count_2{
	
	static int count_2 =0;
	
	public static void main(String[] args){
		
	    int num=20;
	    
	    for(int i=0; i<=num; i++){
	    	count_2 = count2_recur(i);
	    }
	    
	    System.out.println("Count of 2 is:"+count_2);
	}
	
	public static int count2_recur(int num){
		
		if(num == 2 ){ count_2++;}
		else{
			int a = num % 10;
			if(a==2){
				count_2++;
			}
			num = num/10;
			if(num > 0 ){
				count2_recur(num);
			}
			else{
				return count_2;
			}
			
		}
		return count_2;
	}
	
}

- Srinivasan August 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Brute-force

- Anonymous August 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

can u be specific about range?

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

Im not sure what the WAP means for this question. Are you just trying to count how many times the number can be divided by 2? That seems too easy

- houseUrMusic August 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

houseUrMusic,

Write A Program is abbreviated to WAP

- alregith August 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this?
You can prepare a Known list till the max range,
1-10 has 1 occurrence
10-100 has (1*10 + 9) 19
100-1000 has (19*10 + 99) 289
1000-10000 (289*10 + 999) 3889 and so on

n=43
#Will have a limitation of n upto 99999
known_range={1:0,10:1,100:19,1000:289,10000:3889}
base=1
numoftwos=0
while(n>1):
        val=n%10
        for i in range(0,val):
                numoftwos += known_range[base]
                if i==2:
                        numoftwos += base
        base *= 10
        n = n/10
print "n has %d of 2's"%numoftwos

- Cloudster August 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

100-1000 will be 8*19 +100 because there are eight blocks like 100-199, 300-399...and so on until 999 and one block of 100 from 200-299.

- Praveen August 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a recursion algorithm

int recurse(int number){
	if(number < 2){
		return 0;
	}
	if(number > 2 && number < 12){
		return 1;
	}
	int n = 0;
	int temp = number;
	int power = 1;
	while(temp > 0){
		temp = temp/10;
		power = power * 10;
	}
	power = power /10;
	int quotient = number/power;
	
	if(quotient < 2 ){
		return quotient * recurse(power - 1) + recurse(number%power);
	}
	if(quotient == 2){
		return quotient * recurse(power - 1) + number%power + 1;
	}
	if(quotient > 2){
		return (quotient-1)*recurse(power - 1) + (power) + recurse(number%power);
	}
	
}

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

I would prefer to have predefined number of 2s for a range (lets less than 100 and less 1000 etc). The max input is just max of unsigned long or so. So, result can give in constant time (limited number of iterations). But this approach needs quite number of condition checks

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

// i think its a cheap method...but it'll work efficiently...
#include<iostream.h>
void find(int b)
{ int i,j,r,c=0;
for(i=0;i<=b;i++)
{ j=i;
while(1)
{ r=j%10; j=j/10;
if(r==2) c++;
if(j/10==0 & j%10==2)
{ c++; break ; }
else if(j/10==0) break;
}
}
cout<<endl<<"The number of twos are: "; cout<<c;
}


int main()
{ int b; char z;
while(1)
{ cout<<"Enter the no.: "; cin>>b; find(b); cout<<endl;
cout<<endl<<"Do u want 2 find for another no ?(y/n) : "; cin>>z;
if(z=='y' || z=='Y')
{}
else
break ;
}
return(0);
}

- Aniket Roy Chowdhury August 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

import java.util.Scanner;

public class NumtoAlpha {

public NumtoAlpha() {
}
public static void main (String[] args) {

int ipn,n,t,s,r=0;
String c;
char ch;
String str,rev;
str="";
rev="";
Scanner in= new Scanner(System.in);
ipn=in.nextInt();
n=ipn;
while(n>0)
{
t=(n-1)%26;
n=(n-1)/26;
t=65+t;
ch=(char)t;
str= str+ch;
}
s=str.length();
for(int i=s-1;i>=0;i--)
{
rev=rev+str.charAt(i);
}
System.out.print(rev);



}

}

- Syed August 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

ignore above code wrongly paste

- Syed August 02, 2013 | Flag


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