## Amazon Interview Question for Software Engineer / Developers

• 0

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) {
Set<Integer> values = new HashSet<Integer>();
for(int i:a) {
}
for(int i:b) {
if(values.contains(i)) {
}
}
return buildArrayFromList(intersection);
}

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

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

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.

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

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.

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

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

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]);
}
}

}``````

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>();
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)){
if(map.get(i) == 1)
{
map.remove(i);
}else{
int tmp = map.get(i);
map.remove(i);
map.put(i, tmp - 1);
}

}
}

return intersection;

}

}

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).

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++)
for(int i=0;i<b.length;i++){
else
checkDups.remove(b[i]);
}
for(Integer abc:dups)
System.out.println(abc);
}``````

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)
{
}
for(int i:secondList)
{
{
}
}
for(int i:resultSet)
{
System.out.println(i);
}
}

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)``````

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;
}

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

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.

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++){
}
for(int j=0;j<b.length;j++){
if(listA.contains(b[j])){
}
}
}``````

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.

### 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.