Amazon Interview Question for SDE1s


Country: India
Interview Type: In-Person




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

Find all prime No less than n/2.
Print only those pair of found prime No. whose product is less than n

- Anonymous October 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Yes, and to reduce the complexity of finding is a number is prime or not use sieve of Eratosthenes( check out en.wikipedia.org/wiki/Sieve_of_Eratosthenes) to generate all prime numbers upto n/2

Then you can use two for loops { i (0 to len(primes)-1) and j (i to len(primes -1))} to print only those pairs where i*j<=n
the loop -> j (i to len(primes -1)) ensures no repetition and reduces time to check redundant solutions

Python implementation for the same:

def generatePrimes(n):
    sqrtn = int(n**0.5)
    sieve = [True] * (n+1)
    sieve[0] = sieve[1] = False
    for i in range(2, sqrtn+1):
        if sieve[i]:
            m = n/i - i
            sieve[i*i:n+1:i] = [False] * (m+1)
    sieve = [i for i in range(n+1) if sieve[i]] 
    return sieve

def printPairs(n):
    primes = generatePrimes(n/2)
    for i in range(0, len(primes)):
        for j in range(i+1, len(primes)):
            if (i*j)<=n:
                print primes [i], primes[j]

#Call from the driver function 
printPairs(100)

- vik October 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 5 vote

void printLessThanPrimeset(int n){
if (n<4) return;
for (int i=2; i<n; i++){
if (isPrime(i)){
for (int j=n/i; j>=2; j--){
if (isPrime(j)){
cout << i << "x" << j << "=" << i*j << "<=" << n << endl;
}
}

}
}
return;
}

bool isPrime(int n){
if (n==1) return false;
if (n==2) return true;

for (int i=2; i<n; i++){
if (n/i-double(n)/double(i)==0) return false;
}
return true;

}

- IceLatte October 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can implement a sieve for isPrime

- alex October 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

What does "pair" mean? |p-q| = 2 ?

You can sieve out composites from [2,n], then print pairs.

UPDATE====
Oh wow down vote makes me cry so hard :)
Yeah it says p*q <=n, not p, q <=n , misread.

So you can save on the range [2,n].

And only consider [2, n/2] to sieve on.

Let the OP just do it on [2,n] as a first coding step (his/her final check on pq <=n will take care of correctness). Can someone explain to him why n/2, as I will now:

Because , the boundary/extreme case is:
------------------------------------------------------

(smallest possible prime)*(largest possible) <=n

And (smallest possible prime) = 2, so:

2*(largest possible prime) <=n

which implies (divide both sides by 2):

(largest possible prime) <= n/2

Anyone care to explain the complexity of this algorithm?

- S O U N D W A V E October 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

How does this question is different from finding out the prime numbers till n/2.
2×q<=n
So 2<q<=n
So it boils to finding prime numbers from 2 to n/2.

- aka[1] October 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Plz explain in some detail

- Say October 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

+1
Short on detail and confusing, but correct when I read your mind.

- S O U N D W A V E October 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

If n is a prime number then you have to consider 1*n case also.

- GT1 October 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Yeah. I wouldn't bother worrying if someone included 1 as a prime.
A lot of mathematicians in the past included 1, and some still do.

1 is excluded to make theorems like uniqueness of prime factorization "easier" to state.

- S O U N D W A V E October 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

k

- Anonymous October 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 5 vote

private void printPrimePairs(int N) {
		if (N <= 5)
			System.out.println("No Pairs exist");

		for (int i = 2; i <= (N / 2); i++) {
			if (isPrime(i)) {
				for (int j = i + 1; j <= (N / 2); j++) {
					if (isPrime(j) && (i * j) <= N) {
						System.out.println(i + " " + j);
					}
				}
			}
		}
	}

	private boolean isPrime(int i) {
		int j = 2;

		if (i == 2)
			return true;

		while (j < i) {
			if (i % j == 0) {
				return false;
			}
			j++;
		}
		return true;
	}

- Anonymous October 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anonymous:: You can store the results of isPrime() in hashmap.Check if the number is present in hashmap then just return true otherwise go ahead and check if it really a prime number or not.

- anish October 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

// find all pairs of p,q prime numbers such that p * q <= n

// find all prime numbers < = n / 2
// find pairs of prime numbers that satisfy the condition from earlier collected numbers

bool isPrime(int n)
{
	if (n <= 1)
		return false;
	
	if (n == 2)
		return true;
	
	if(n > 2 && n %2 == 0)
		return false;
		
	for(int i = 3; i < n / 2; i += 2)
	{
		if(n % i == 0)
			return false;
	}
	return true;
}

void printPairs(int n)
{
	vector<int> primes;
	for(int i = 1; i <= n; i++)
	{
		if(isPrime(i))
			primes.push_back(i);
	}
	for(int i = 0; i < primes.size(); i++)
	{
		for(int j = 0; j < primes.size(), i != j; j++)
		{
			if(primes[i] * primes[j] <= n)
			{
				cout<<primes[i]<<" "<<primes[j]<<endl;
			}
		}
	}
}

- Dave October 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isPrime(unsigned int n, vector<unsigned int> &primes) {

   for(auto iter i = primes.begin() ; i!= primes.end() ;++i) {
       if ( ! n%*i ) return false;
   }

   return true;
}

void findPairs(const uint n, set<pair<unit,unit>> &products) {

   vector<unsigned int> &primes ;

   for(uint i = 2 ; i<= n/2 ; ++i) {
      if( isPrime(i, primes)) 
        primes.puch_back(i);
   } 


   for(vector<uint>::reverse_iterator iter i=primes.end() ; i!= primes.begin() ;++i) {
          for(vector<uint>::iterator iter j=primes.begin() ; j!= primes.end() ;++j) {
                  if( *i* *j == n && products.find(make_pair<*j,*i>) == products.end() ) { 
                            products.insert( make_pair(*i,*j) );
                  }
                  if (*i * *j > n) continue;
          }
    }

}

- codemonkey October 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static bool IsPrime(int n)
        {
            int limit = (int)Math.Sqrt(n) + 1;
            for (int i = 2; i < limit; i++)
            {
                if (n % i == 0)
                {
                    return false;
                }
            }
            return true;
        }

        static void SearchPrimePairs(int n)
        {
            for (int i = 2; i < n / 2 + 1; i++)
            {
                if (IsPrime(i))
                {
                    for (int j = 2; j < n/i + 1; j++)
                    {
                        if (i * j <= n)
                        {
                            if (IsPrime(j))
                            {
                                Console.WriteLine("{0}:{1}", i, j);
                            }
                        }
                    }
                }
            }
        }

- little less in the fors? October 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

//C# with a little improvement (taking primes out and reusing in IsPrime) to Dave's C++ code 
// find all pairs of p,q prime numbers such that p * q <= n
// find all prime numbers < = n / 2
// find pairs of prime numbers that satisfy the condition from earlier collected numbers
private static ArrayList primes;

static bool isPrime(int n)
{
	if (n <= 1)
		return false;

	if (n == 2)
		return true;

	foreach (int i in primes)
	{
		if (n % i == 0)
			return false;
	}
	return true;
}

static void printPairs(int n)
{
	primes = new ArrayList();
	primes.Clear(); //Remove all elements from the ArrayList. This could be improved to remove elements larger than n/2 if needed
	int max = n / 2; //for performance
	for (int i = 1; i <= max; i++)
	{
		if (isPrime(i))
			primes.Add(i);
	}
	foreach (int i in primes)
	{
		foreach (int j in primes)
		{
			if (i * j <= n)
			{
				Console.WriteLine(String.Format("({0}, {1})", i, j));
			}
		}
	}
}

- IC October 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 4 vote

Use the Eratosthene's sieve algorithm only as URIK LAGNES suggested. Just find primes from 1 to sqrt(N) store them in a set.
All pairs from this set should be printed as answers.

- Learner October 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

@learner:why only till square root of n?

- aka[1] October 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

should be upto n/2 not square root of n ...
Say n=16, then square root will give 4 .. so output will be 1,2; 1,3;2,3 in your algo ..
However output needed should also have 1,5;2,5;3,5;1,7;2,7;

- Jack October 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

He meant one of the loops in EratSiev need only go upto sqrt(largest prime you are trying to find)

- S O U N D W A V E October 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You should point out the trade-offs of Eratosthenes. It is faster than checking each number but it takes O(N) space. Wikipedia describes an incremental version which takes less space.

- galli October 11, 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