Amazon Interview Question for Software Engineer / Developers






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

Adding up all numbers as mentioned by S is good but cannot be implemented as addition of so many numbers will result in overflow..

Instead, all numbers can be xored...

ie
for numbers 1..5,
(1^2^3^4^5) ^ (3^4^1^5) will give the missing number 2

- saptarshi December 09, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please give the code for this XOR example?

Also, the solution by S will work if you use an unsigned long or unsigned long long (system-dependent). The sum of all ints from 1 - 100000 is only 5000005000000.

- cacci May 06, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

works only with many assumptions:
try ex1:
{1,2,4,4,5,6}, it has duplicate number and missing number.

- bbs59 July 18, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

ANSWER: add up the sum of all expected values, then subtract the sum of all values in the array and you have your missing number. O(n) runtime.

- S November 12, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is a good solution if there is only one number is missing

- Longge November 14, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if the sum is too huge, that no primitive type can hold it?
and what if you just have O(1) space?
what if you have two missing number?

- bbs59 July 18, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

use bitmap

- ss November 12, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

check programming pearls, chapter 1. Binary method

- keshav November 12, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What is the Binary method?

- cecil November 15, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

boolean bools(1..1 million)
foreach element e in bools
e = false
endfor

foreach element e in array
bools(e) = true
endfor

foreach element e in bools
if e equals false
println "THE MISSING NUMBER IS ", e
endif
endfor

Above technique can be used to find out not just 1 but several missing numbers. It works even if original array has all numbers missing!

- coboler November 16, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@coboler - This technique is the bitmap technique and it is a great solution. But you would need a lot of memory available to you to do this. Do you know of a solution that is memory efficient?

- Jay November 18, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if the number are in sorted order and, we know the lengh, we can find with in O(log(n)) complexty

keep deviding the length and number with 2, and verify lengh of both partitions.

- Anonymous November 21, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good thinking.

- Anonymous July 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes, doing a XOR is a good option.

Here is a (hopefully) fun puzzle related to this.

Give a formula for 1 XOR 2 XOR 3 XOR .... XOR n.

Basically, give a constant time algorithm to calculate the XOR of first n natural numbers.

- T March 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if more than one number is missing?

- Erik March 22, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Question says, _the_ missing number, so I expect only one number is missing. It seems to also imply that the other numbers appear in the array.

- T March 22, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The XOR solution will work only if N (the total number of numbers) is of the form 2^K-1.

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

Of course we can always find the number 2^K-1 >= N and implicitly treat them as present

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

cocoler has de right soln

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

*coboler

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

We make use of the XOR operator as follows:

axor = the n values in the array (a[0]^a[1]^...^a[n-1])
vxor = the consecutive values from 0 to n (n+1 values) (0^1^...^n).

At the end axor^vxor gives the missing number since all other numbers cancel each other out.

int missingNumber(int *a, int n)
{
	int axor=0, vxor=n;
	for(int i=0; i<n; i++)
	{
		axor ^= a[i];
		vxor ^= i;
	}
	return (axor ^ vxor);

}

Thanks!

- catalin4ever December 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about:

(1):Bloom filter OR
(2): n(n+1)/2 formula

- Anubhav December 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using the xor technique we will get the missing number, find the xor of numbers 1 to 1 million and then find the xor of all array elements and then, finally find the xor both the previously xor elements.

- swapnilkant11 July 20, 2019 | 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