Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

This is my first thought (I'm sure it can be improved, but as a first solution):
int [] intersection(int [] a, int [] b) {
List<integer> intersection = new LinkedList<Integer>();
Set<Integer> values = new HashSet<Integer>();
for(int i:a) {
values.add(i);
}
for(int i:b) {
if(values.contains(i)) {
intersection.add(i);
}
}
return buildArrayFromList(intersection);
}

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

I would use Set<Integer> for intersection. Otherwise duplicated elements in b would result in duplicated elements in intersection.

- Bob November 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I would use a hash table and add numbers of both arrays to the hash table keeping count of the occurrences. If count is more than 1, we add that element it to the intersection array. This gives O(m+n) efficiency.

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

Those two arrays are not necessary unique. Your implementation will take any integers appear two or more time in the same array, but not in the other array. That output isn't correct.

- fz November 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

when inserting to the hash table if the count is exactly 0 then add to the intersection array.

- Faisal December 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

public static void findIntersection(int[] arr1, int[] arr2){
		for(int i =0; i<arr1.length; i++ )
		{
			for(int j = 0; j<arr2.length; j++){
				if(arr1[i] == arr2[j])
				System.out.println(arr1[i]);
			}
		}
		
	}

- code_freak November 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my solution. Please let me if it fails to for any scenario.

public class IntersectionOfArrays {

public static void main(String[] args) {
int[] A = {3,6,1,13,4,3,8,9,1,5,2};
int[] B = {13,4,1,3,9,17,12,25,6};

ArrayList<Integer> c = findIntersection(A,B); //O(m+n)
for(Integer i : c){
System.out.println(i +",");
}
}

public static ArrayList<Integer> findIntersection(int[] A, int[] B){
ArrayList<Integer> intersection = new ArrayList<Integer>();
// Add A into hashMap
HashMap<Integer,Integer> map = new HashMap<Integer, Integer>();
for(int i : A){ // O(m)
if(map.containsKey(i))
{
int tmp = map.get(i);
map.remove(i);
map.put(i, tmp+1);
}else{
map.put(i, 1);
}
}

// Now start compare with B array

for(int i : B){ // O(n)
if(map.containsKey(i)){
intersection.add(i);
if(map.get(i) == 1)
{
map.remove(i);
}else{
int tmp = map.get(i);
map.remove(i);
map.put(i, tmp - 1);
}

}
}

return intersection;

}

}

- om November 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sort one of d array(m elements would take log m time) and using binary search, find if elements in 2nd array are in d first one(n elements would take nlogm time). If yes copy them in a 3rd array. complexity would be (logm+ nlogm).

- cravi24 November 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void findInterSection(int a[], int[] b){
		HashSet<Integer> checkDups=new HashSet<Integer>();
		HashSet<Integer> dups=new HashSet<Integer>();
		for(int i=0;i<a.length;i++)
			checkDups.add(a[i]);
		for(int i=0;i<b.length;i++){
			if(!checkDups.add(b[i]))
				dups.add(b[i]);
			else
				checkDups.remove(b[i]);
		}
		for(Integer abc:dups)
			System.out.println(abc);
	}

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

I think the best way to do this is to use the sets

public static void setFunction(List<Integer> firstList,List<Integer> secondList)
{
Set<Integer> firstSet=new HashSet<Integer>();
Set<Integer> resultSet=new HashSet<Integer>();
for(int i:firstList)
{
firstSet.add(i);
}
for(int i:secondList)
{
if(!firstSet.add(i))
{
resultSet.add(i);
}
}
for(int i:resultSet)
{
System.out.println(i);
}
}

- Arunav Borthakur December 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def intersect(arr: Array[Int], a1: Array[Int], a2: Array[Int], i: Int): Array[Int] = a1 match {
  case Array() => arr
  case Array(x, a@_*) if(a2.indexOf(x) < 0) => arr(i) = 0;intersect(arr, a.toArray, a2, i+1) 
  case Array(x, a@_*) => intersect(arr, a.toArray, a2, i+1) 
 }

val arr = Array(1,2,3,4,5,6)
scala> val r = intersect(arr ,arr, Array(2,3,4,6,7,8,9,12,34), 0).filterNot(e => e == 0)
r: Array[Int] = Array(2, 3, 4, 6)

- rbsomeg February 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Here is a solution provided the arrays are sorted:

vector<int> findIntersection(vector<int> A, vector<int> B) {
vector<int> intersection;
int n1 = A.size();
int n2 = B.size();
int i = 0, j = 0;
while (i < n1 && j < n2) {
if (A[i] > B[j]) {
j++;
} else if (B[j] > A[i]) {
i++;
} else {
intersection.push_back(A[i]);
i++;
j++;
}
}
return intersection;
}

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

The question doesn't say the two input arrays are sorted. And question asks you to write the function implementation, but not to have your own function interface.

- fz November 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

private static void intersection(int[] a, int[] b) {
		List<Integer> listA = new ArrayList<Integer>(); 
		Set<Integer> intersectedElements = new HashSet<Integer>();
		for(int i=0;i<a.length;i++){
			listA.add(a[i]);
			System.out.println("added.. "+listA.get(i));
		}
		for(int j=0;j<b.length;j++){
			if(listA.contains(b[j])){
				intersectedElements.add(b[j]);
			}
		}
	}

- Ankita N November 22, 2012 | 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