Amazon Interview Question for Software Engineer / Developers






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

(sum of 1 to 1000) - (sum of 999 distinct integers) = missing number

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

yup, cant think of nything else

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

XOR.

- Xorro November 07, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@xorro. can u plz elaborate ur ans..when u say cumulative xor do u mean xor of the individual bits..how would that give the index.plz explain

- Stag January 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

But it says - can't allocate an additional array.

- Garry November 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

(sum of 1 to 1000) = (1000*1001)/2 = 500500

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

boolean flag = false;
for (int i = 0; i < a.length; i++) {
if (i + 1 == a[i]) {
continue;
} else {
flag = true;
System.out.println(i + 1);
break;
}
}
if (!flag) {

System.out.println("1000");
}

- Garry November 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am assuming here array is sorted.

- Anonymous November 07, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code only handles a sorted array, the question does not say that the input is sorted

- Anonymous October 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

array should be sorted, if not then this code will not run !

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

Cumulative XOR of numbers in the array and the their index will give the missing number in the end. Time Complexity O(n). Space complexity O(1)
Note: The sum method suffers with an overflow problem in the general case.

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

I don't think this will give the solution. Consider number from 1-5, now 5 is missing. Xor of 1 to 4 is 4.

- Gajanan November 12, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@anonymous. can u plz elaborate ur ans..when u say cumulative xor do u mean xor of the individual bits..how would that give the index.plz explain

- stag January 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

the array need not be sorted and the problem can be solved in O(n)
we know the formula, for sum of n numbers from 1 to n is n*(n+1)/2
code:
int missing_number(int len, int a[])
{
sum=len*(len+1)/2;
for(int i=0; i<len;i++)
{
sum=sum-a[i];
}
return (sum);
}

- T November 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming that number are not sorted and numbers are between 1 and 999.
now subtract the first number with 1000. do this with all the element of the array and insert the reminder in a hash table.
Before subtraction, find the element in the hash table and delete it if found. At last you will left with the missing number.
there might be some more correction.. but this is the heart of my algo.

- kk November 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No additional data structures

- Arihant November 30, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sum of n numbers = n(n+1)/2 : 1000(1001)/2 = 500500

Subtract the total sum of the given number from 500500.

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

suppose array is {3,5,1,4} and range is 1-5 missing numeber is 2

so XOR of array is 3^5^1^4
and XOR OF RANGE is 1^2^3^4^5
because XOR of two number is 0 ....

(1^2^3^4^5)^(3^5^1^4)=2 result

- ridercoder November 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The very efficient way to find the missing integer from the array we will first xor all the numbers from 1 to n, then we will xor all the array element numbers and then at the end we will xor the both of them to get the missing number in the array.

Implementation:

int findmissing(int arr[], int n){
	int xor_number = 1;
	int xor_array = arr[0];
	for(int i = 2; i <= n + 1; i++)
		xor_number = xor_number ^ i;
	for(int i = 1; i < n; i++)
		xor_array = xor_array ^ arr[i];
	return xor_array ^ xor_number;
}

- swapnilkant11 July 22, 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