Amazon Interview Question for Software Engineer / Developers






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

C ++ Version :

int n, m;

cout << "Please enter the #: " << endl;
cin >> n;

m = n +1;

for(int i = 2; i <= m/2; i++)
{
if(m%i == 0)
{
m = m + 1;
i=2;
}
}

cout << "M is: " << m << endl;

- Nick N October 08, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int n, n1, itr;
printf("\n\tEnter n: "); scanf("%d",&n);
n1=n+1;
while(1)
{
for(itr=2;itr<=n1/2;itr++)
if(n1%itr==0)break;
else continue;
if(n1/2<=itr)break;
else n1++;
}
printf("\n\n\tNext prime no is: [%d]",n1);

- Illusionist October 03, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if(n1/2<=itr)break;
remove = from this condition otherwise if you enter 3 you will get next prime no 4

- anonyms October 06, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Improvements to Illusions code/

// Consider only odd numbers after n.
int n, n1, itr;
printf("\n\tEnter n: "); scanf("%d",&n);

// take next odd number after n.

if(n%2==0) n1=n+1
else n1=n+2;

while(1)
{
for(itr=2;itr<=n1/2;itr++)
if(n1%itr==0)break;
else continue;
if(n1/2<=itr)break;
else n1=n1+2; // only consider odd numbers after n.
}
printf("\n\n\tNext prime no is: [%d]",n1);

- Ravikumar October 08, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

there is an addition trick to use; instead of only dividing by odd number, you only need to divide by all of the prime numbers < n.

- kev April 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Instead of using n1/2 as the check condition, it is better to use squareroot(n1)

- A April 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this link shows how it can be done right -
http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/prime-2.html

- jr June 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

thnaks jr

- dev September 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. x=sprt(n)
2. check all prime numbers <= x (you do not have to go up to n/2)
if n can not be divided by any of these prime numbers, then it is also a prime

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

Ruby code:

def next_prime(number)
  next_number = number % 2 == 0 ? number+1 : number+2

  while true
    2.upto(next_number/2) do |i|
      next_number % i == 0 ? break : i
      return next_number if i >= next_number/2
    end
    
    next_number += 2
  end
end

- sepsep3 May 02, 2015 | 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