Amazon Interview Question for Software Engineer / Developers






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

Use seive of eratosthenes algorithm.
O(Nlog(N))

- akshay May 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

int main()
{
int n;
cout<<"\nEnter the number:";
cin>>n;

int arr[n+1];
for(int i=1;i<n;i++)arr[i]=i+1;
arr[0]=0;arr[n]=0;
int prime[n];
for(int i=0;i<n;i++)prime[n]=0;
int k;
int p=0;prime[p]=2;
int t=1;
while(arr[1]!=0)
{
k=1;
while(arr[t]!=0)
{

if(arr[t]%prime[p]!=0)
{
arr[k++]=arr[t++];
}
else t++;
}
arr[k]=0;

prime[++p]=arr[1];
t=1;
}
cout<<"\nThe Prime numbers till "<<n<<"are as follows:";
for(int i=0;i<p;i++)cout<<endl<<prime[i];
cin>>k;
return 0;
}

the time complexity of the algo is O(nloglogn) and space complexity is O(n).
still not sure of exact time complexity. any idea??

- Anonymous May 30, 2011 | 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