Amazon Interview Question for Software Engineer / Developers






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

Use XOR

public class Missing_and_Duplicate {

/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
int[] intArray2={2,4,7,5,3,1}; // 6 is missing
find_missing(intArray2);

}

public static void find_missing(int[] intArray)
{
int answer = intArray[0];
for(int i=1;i<intArray.length;i++)
{
answer = answer ^ intArray[i];
}
System.out.println("answer="+answer);
}

}

- jj February 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class hello {
public static void main(String args[]){
int [] arr = {2,4,5,1,7,9,6,8,10,3,12,13, 11, 15}; // 1to15, 14 missing
findmissing(arr);
}

private static void findmissing(int[] arr) 
{
	int []temp = new int [arr.length+2];
	for(int data : arr)
   {
	temp[data]= 1;
   }
	
  for(int i = 1; i < temp.length; i++)
 { 
		if (temp[i]==0)
            {
		System.out.println(i);
	    }
 }

}
	
	
	
}

I don't like using a temp array, I was in a hurry to solve, further optimizations can be done.

Thanks

- rajat February 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sum all number in the array and subtract from (N*(N+1))/2 if 'N-1' is the size of the array.

- Mahesh February 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

wonderful!!

- Jovi June 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Overflow may cause problem

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

we can use xor..:)

- sg February 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

n(n+1)/2 - Sum of all elements in the array.

- Sumeet February 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Mahesh is correct!

- Anonymous February 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

or keep diving every number by N!. Residue is the number which did not appear.

- Anonymous February 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

or keep dividing every number by N!. Residue is the number which did not appear.

- Anonymous February 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

or keep dividing every number from N!. Residue is the number which did not appear.

- Anonymous February 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

how can we use XOR

- mariselvam February 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@sg

how can we use XOR ..?

- mariselvam February 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Elements missing in a range
int [] arr = {2,4,19, 18, 15,-15};
Arrays.sort(arr);
for (int i=arr[0],j=1;i<arr[arr.length-1];i++) {
if (i < arr[j-1]) {
System.out.println("Missing Element"+ i);
}
else {
j++;
}
}

- tparakka April 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Int overflow is a problem

- Anonymous March 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The most efficient algorithm to find the missing number in the array is to use the concept of xor we will xor all the array elements and store them in one variable and we will xor all the numbers from 1 to n and store them in one variable and then next we will xor both of them which will return our missing element.

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