Amazon Interview Question: We have numbers from 1 to 52 ... | Glassdoor

Interview Question

Software Engineer Interview Seattle, WA

We have numbers from 1 to 52 that are put into a 51 number

  array, what's the best way to find out which number is missing?
Answer

Interview Answer

4 Answers

2

my answer was to sort the array and use a boolean variable with a while loop that will exit when the number is not equal to the previous number plus 1

Interview Candidate on Jun 17, 2010
10

The above answer is a typical answer or brute force technique, which anyone could answer. What they are looking for is probably how good you are at math and algorithms. My answer would be:

1) Calculate the sum of all numbers stored in the array of size 51.
2) Subtract the sum from (52 * 53)/2 ---- Formula : n (n+1)/2.

The result of subtraction is the answer for this question.

Babji Chetty on Jun 19, 2010
3

Just want to add some comments to Babji Chetty's answer.

To calculate sum of complete sequence of 1 to 52 is used arithmetic progression sum formula. s = n(2a1+d(n-1))/2, where d is the step. Using this formula you can calculate sum of full sequence even if increment step not equal to 1.

Oleh on Jul 8, 2010
1

Here is the solution for finding all the missing numbers from a given array: http://stackoverflow.com/a/26575716/4052544

Bhumik on Oct 26, 2014

Add Answers or Comments

To comment on this, Sign In or Sign Up.