Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

1. If the number is less than 2 return false.
2. If the numbers is 2 return true.
3. let x = square_root_of (n)
4. for i = 3 to x
5. if ( x % number == 0) return false;
6. return true;

Why Square root? (step 3)
..... because for any non prime number, one of the factors is <= the square root.

Why less than 2? (step1)
......negative numbers and zero are not prime
......1 is not prime

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

if in step 2, we also check if((number % 2) == 0) return false. then we can avoid the looping from 3 to sqrt(number). just a minor optimization

- Jester January 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include<stdio.h>

int main()
{
	int i, n, flag;
	int str[10];
	printf("\nEnter the number\n");
	scanf("%d", &n);
	flag = 1; //----------------Assuming that the number is prime-----//
	for(i = 2; i <= n/2; i++)
	{
		if(n % i == 0)
			flag = 0;
	}
	
	if(0 == n || 1 == n || flag == 1)
		printf("\n%d is a prime number\n", n);
	else
		printf("\n%d is a not prime number\n", n);
}

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

int str[10]; was written mistakenly... It is of no use in the program. But the program works fine for any input.

- Prathamesh January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

program works fine for any input.

really? I mean, REALLY? Your code is completely thrown off for large numbers and negative numbers.
PS: Take time, read the question and see if you need return something or SHOUT that you got output. And before claiming something big, be prepared to back it up!

- AmzFAILFacebookFailMSFTFail January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Negative numbers are not prime numbers. Also, by adding just one "if sentence", i.e. if the entered number is less than zero, then it is not a prime number, can answer above question.
Regarding the large numbers, it is just a matter of computation that it may take extra time.
But the concept behind the code seems to be alright.

- Namrata January 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isPrime(int num)
	{
		if(num<=0) 
		{
			System.out.println("not a natural number");
			return false;
		}
		if(num==1)
			{return true;}
		int limit=(int) Math.sqrt((double)num);
		for(int i=2;i<limit;i++)
		{
			if(num%i==0)
			{
				return false;
			}
		}
		return true;

}

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

You have done a very common mistake. Your function will fail for perfect squares 49,64,81 etc etc. You must change your loop to check for i<=sqrt(number)

- Rayden January 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isPrime(int num)
	{
		if(num<=0) 
		{
			System.out.println("not a natural number");
			return false;
		}
		if(num==1)
			{return true;}
		int limit=(int) Math.sqrt((double)num);
		for(int i=2;i<limit;i++)
		{
			if(num%i==0)
			{
				return false;
			}
		}
		return true;

}

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

for(int i=2;i<limit;i++) ... Following for loop condition in above code should be modified as for(int i=2;i<=limit;i++)....check for num = 25.

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

All integers can be expressed as (6k + i) for some integer k and i = -1 0 1 2 3 4.

Integers of the form (6k + 0), (6k + 2) and (6k + 4) can be divided by 2. Integers that can be express as (6k + 3) can be divided by 3. Hence:

public boolean isPrime(int n) {
        if (n <= 1)
            return false;
        if (n <= 3) {
            return true;
        }
        if (n % 2 == 0) {
            return false;
        }
        if (n % 3 == 0) {
            return false;
        }

        int squareRootOfN = (int) Math.sqrt(n);
        int x = 5;

        while (x <= squareRootOfN) {
            if (n % x == 0)
                return false;
            x+=2;
            if (n % x == 0)
                return false;
            x+=4;
        }
        return true;
    }

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

^ best algorithm if the input is a large number.(

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

#include<math.h>

int isPrime( int n)
{
int i;

for( i = 2; i <= sqrt(n); i++)
if( n%i == 0)
return 0;
else
return 1;
}

int main()
{
if(isPrime(16))
printf("Yes it's a prime !!");
else
printf("No it's not a prime !!");
return 0;
}

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

bool checkPrime(int n)
{
if(n==1) return false;
for(int i=2;i<=sqrt(n);i++)
{
if((n%i)==0) return false;
}
return true;
}

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

import java.lang.Math.*;
import java.io.*;
public class Prime
{

public static Boolean primeNumber(long x)
{
Boolean flag = true;
if(x<4)
{
if(x==1)
flag=false;
}
else
{
for(int i = 2; i<(int)Math.sqrt(x)+1;i++)
{
if(x%i==0)
{
flag=false;
}
}
}
return flag;
}
public static void main(String args[])
{
Boolean flag;
flag =primeNumber(Long.parseLong(args[0]));
System.out.println(flag);
}
}

- Haomin February 24, 2012 | 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