Amazon Interview Question for Software Engineer in Tests






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

the most efficient solution is using hashtable for the 1st array.

- Anonymous July 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In that case the complexity will be O(n+m) because first we put all the elements of array 1 in the hashtable and then we check the second array.

- Anonymous July 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

u r right...first place all the 1st array elements in the hash table and then try to place the 2nd array elemnts into the same hash table....the hash key element which has a value 2 is the intersection element...O(n).

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

#include <set>
#include <algorithm>
#include <iostream>

using namespace std ;

int main() {

	int a[] = {1,2,3,4,5,6} ;
	int b[] = {3,4,7,1,9} ;
	
	set<int> mySet(b, b+5) ;
	
	cout << "\n Intersection :\t " << std::endl ;
	
	for(int i=0 ; i<6; ++ i ){
		if(std::binary_search(mySet.begin(), mySet.end(),a[i] ) )
			std::cout << a[i] << "\t" ;
	}		

	return 0 ;
}

- Mahesh July 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The interviewer needed a solution code using hashtable...can anyone write the code usage..i coudnt. Thanks.

- careersaa July 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

To use hashtable, you should know the range of elements present in the array, I think.

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

He asked me to consider a general input, not a given arrays.

- careersaa July 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int findDup(int arr[], int arr2[]){
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i = 0;i<arr.length;i++){
map.put(arr[i],i);
}


for(Integer i :arr2){
if(map.contains(i))
return map.get(i);
}

return -1

}

- cacheMachine July 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thanks. And can the union of the two arrays be implemented with hashtables? If so, can you please give me the implementation for it.

- careersaa July 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@cacheMachine..I understand in your code is that you are creating a hashmap called map and adding all the arr[] values into it. When you write map.put(arr[i],i); , are the array elements added as keys or values?

And are you checking if the keys are same or the values as you have not mentioned if its is map.containskey() or map.containsvalue().

- careersaa July 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you can use map.put(arr[i], arr[i])
so now it contains the same key and value and u can check with if(map.containsKey(arr2[j]))

- anonymous July 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

somebody plz tell me ...wht is meant by intersection of arrays??

- Anonymous July 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

intersection of arrays means same numbers in both array. as in 'intersection of sets'.

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

package practice;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Set;

public class IntersectionOfArrays {

public static void main(String[] args) {
// TODO Auto-generated method stub
int [] arr = {3,6,7,4,9};
int [] brr = {2,8,1,7,0};
Hashtable ht = new Hashtable();
for(int i=0; i<5; i++)
{
ht.put(arr[i], i);
}
for(int i=0; i<5; i++)
{
int a;
a= brr[i];
if(ht.containsKey(a))
{
System.out.println("the array intersect");
}
else
{
System.out.println("the array does not intersect");
}
}

}

}

- Nam January 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Sort one of the arrays with quick sort O(n*Log(n)).
Loop through the other array and if item exists in the other array include it in result(O(n*Log(n))

The algorithm will be O(n*Log(n)

- Anonymous July 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you loop the other array you have to traverse the full array. That means it is n * nlogn because you will run binary search for each of the item.

- Anonymous July 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Binary search is only Log(n) not nLog(n)

- Anonymous July 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

See carefully he is doing binary search for each element
so log(n) for each element
hence nlog(n)

- anonymus July 25, 2011 | 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