Amazon Interview Question for Developer Program Engineers


Country: India
Interview Type: In-Person




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

Assuming the 2 arrays are very large to it into the memory, use merge sort, and during merging phase discard if duplicates are found.

This will work in O(NlogN) where N = sizeof(array1) + sizeof(array2);

- Praveen Kumar July 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Too large means we can't sort them in memory?

- notbad July 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Oops, My bad, I wrote for a join.
Modifying the flag values for un-common numbers should do he trick :)

- chaos July 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sort each and make union O(nlogn) ?

- Anonymous July 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am not totally sure but this should work.

Store the First array in a hashmap using the integer as key and a flag value of 1.
Now read the second array, and try and find a hit for that value in the first hashmap.
If you get a hit or a flag value of one corresponding to that value from the second array, direct it to the output.

In case the array is very large to fit in memory in one go, read n values at a time.
Complexity If m is the total number of integers in the large array
( m/n )*n. or mn or O(N)

- chaos July 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think the output would be intersection of 2 arrays

- pratap.raavi July 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

I guess this should do

void union(int[] a, int[]b){		
		int smallArray[] = null;
		int largeArray[] = null;
		if(a.length<b.length){
			smallArray = a;
			largeArray = b;
		}else{
			smallArray = a;
			largeArray = b;
		}
		HashSet<Integer> set = new HashSet<Integer>();
		ArrayList<Integer> arr = new ArrayList<Integer>();
		arr.ensureCapacity(largeArray.length+smallArray.length);
		
		for(int i:smallArray)
			set.add(i);
		for(int i :largeArray)
			if(!set.contains(i))
				arr.add(i);
		
		for(int i: set)
			System.out.print(i + " ");
		for(int i:arr)
			System.out.print(i+ " ");
	}

- pratap.raavi July 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I like this one. I think he want contains method of set in java.

- Anonymous July 30, 2012 | 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