Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Use sieve method its more effective but requires additional array of size equals to number of elements between between the range..

- c7c7 October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think more than that...

- piyush November 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include "stdio.h"

#define bool int
#define TRUE 1
#define FALSE 0

bool isPrimNumber(int k)
{
for(int i=2; i<k/2; i++)
{
if(k%i == 0)
return FALSE;
}

return TRUE;
}

main()
{
int a = 16, b = 100;
int i = a;

for(i=a; i<=b; i++)
{
if(isPrimNumber(i))
printf("%d\n\r", i);
}
}

- Bin October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This may be optimized by loop over k=2 to sqrt(k) instead of k/2. It's just a naive way to use sieve method. But no additional space required.

- warrior October 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think there are several faster ways to test prime.
1) you can start testing n/2, n/3, and then test n/(6*k+-1);
2) you can apply Fermat's little theorem;

- amyue.xing November 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Primenik {
	public static void main(String[] args) {

		int temp=0;
		for(int i=1;i<100;i++)
		{
			temp=0;
			for(int j=1;j<i;j++)
			{
				if(i%j==0 & j!=1)
					temp=1;
			}
			if(temp==0)
				System.out.println(i);
		}
		}
}

- don October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

# include<stdio.h>
# include<conio.h>
# include<math.h>
int sieve(int factor,int c[],int size,int a)
{
//i need to chop out all factors of i in the array
//-1 for the number whose factor doesn't exist uptil and 1 for a non-prime number
int i;
if(a==1)
i=1;
for(i=0;i<size;i++)
{
if(c[i]==-1&&(a+i)!=factor&&!((i+a)%factor))
c[i]=1;
}//end of for loop
return 0;
}//end of sieve function
int print(int a ,int b)
{

int size=b-a+1,c[size],i;

for(i=0;i<size;i++)
c[i]=-1;
if(a==1)
c[0]=1;//coz one is not a prime number

for(i=2;i<=(int)sqrt(b);i++)
{
sieve(i,c,size,a);
}//end of for loop
for(i=0;i<size;i++)
{
if(c[i]==-1)
printf("%d\n",i+a);
}
return 0;


}//end of function
int main()
{
int a,b;
printf("this program is made by considering that we have to include a and b as well\n");
printf("enter the lower bound");
scanf("%d",&a);
printf("enter the upper bound");
scanf("%d",&b);
print(a,b);
printf("returned from the function");
getch();
return 0;
}//end of main function

- c7c7 October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

code using method of sieve of eratosthenes

- c7c7 October 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class PrimeNumbersInRange {
public void primeCheck(int a)
{
//System.out.println("check"+a);
for(int i = 2; i<=a; i++)
{
//System.out.println(a%2);
if(a != i)
{
if( a % i == 0)
{
break;
}
}
else
{
System.out.println(a);
}
}
}
public static void main(String[] args) {
PrimeNumbersInRange prn = new PrimeNumbersInRange();
System.out.println("Please enter the range of numbers");
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int init = Integer.parseInt(br.readLine());
int end = Integer.parseInt(br.readLine());
System.out.println("The prime numbers are");
for (;init<end;init++)
{
prn.primeCheck(init);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

- Silambarasan October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;

public class Prime {
public static void main(String[] args) {

Scanner s = new Scanner(System.in);

int a = s.nextInt();
int b = s.nextInt();
int counter;

System.out.println("Displaying Prime numbers between " + a + " and " + b);

for(int i = a; i <= b; i++) {
counter=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
counter=1;
}
if(counter==0) {
System.out.println(i);
}
}
}
}

- Kumar October 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static boolean isPrime(int n)
{
boolean isPrime = true;
if (n <= 0 ||(n % 2 == 0) ||(n == 1))
{
isPrime = false;
}
else if (n == 2)
{
isPrime = true;
}
else
{
for (int i = 3; i <= Math.sqrt(n); i = i+2)
{
if (n % i == 0)
{
isPrime = false;
}
}
}
return isPrime;
}

- Raghava October 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void Main(string[] args)
{
  int n = 1, m = 1000000;
  if (n % 2 != 0) n++;
  for (int i = n + 1; i < m; i = i + 2)
    if (IsPrime(i))
      Console.WriteLine(i);
}
 
public static bool IsPrime(int Num)
{
  double sqrt = Math.Sqrt(Num);
  int N = int.Parse(Math.Ceiling(sqrt).ToString()) + 1;
  for (int i = 3; i < N; i = i + 2)
    if (Num % i == 0)
      return false;
  return true;
}

- Rail.Suleymanov October 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>

using namespace std;
main()
{


int a,b;

cout<<"Enter the Lower limit:";
cin>>a;

cout<<"Enter the Upper Limit:";
cin>>b;


for(int i=a; i<=b; i++)
{

int temp=a;
int temp1=temp/temp;
int temp2=temp % 2;
int temp3=temp % 3;
int temp4= temp % 5;
int temp5=temp % 7;

if(temp1==1 )
{
if(temp2 !=0)
{
if(temp3 !=0)
{
if(temp4!=0)
{

if(temp5!=0)
{

cout<<temp<<"is prime"<<endl;


}

}

}



}
a++;
}


}

return 0;

}

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

def isPrime(n: Int) = (2 until n).forall(d => n % d != 0) 

def primesBtween(a: Int, b: Int): Array[Int] = (b-a) match {
 case 0 | 1 => Array()
 case i: Int if(i > 0) => println(i);(a until b).toArray.filter(isPrime)
 case _ => Array()
}

scala> val primes =  primesBtween(6,50)
44
primes: Array[Int] = Array(7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47)

- rbsomeg February 19, 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