Google Interview Question
1,223 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer at Google:
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (3)
0 of 1 people found this helpful
public ArrayList<Integer> intersection(int list1[], int list2[]) {
int i, max = 0, min = 0;
Hashtable<Integer, Integer> hash;
ArrayList<Integer> list = new ArrayList<Integer>();
if (list1.length > list2.length) {
hash = new Hashtable<Integer, Integer>(list1.length, list1.length);
max = list1.length;
min = list2.length;
} else {
hash = new Hashtable<Integer, Integer>(list2.length, list2.length);
max = list2.length;
min = list1.length;
}
for (i = 0; i < max; i++) {
if (max == list1.length)
hash.put(list1[i], list1[i]);
else
hash.put(list2[i], list2[i]);
}
for (i = 0; i < min; i++) {
if (min == list1.length) {
if (hash.put(list1[i], list1[i]) != null) {
list.add(list1[i]);
}
} else {
if (hash.put(list2[i], list2[i]) != null) {
list.add(list2[i]);
}
}
}
return list;
}
Helpful Answer?
Yes |
No
Inappropriate?
0 of 1 people found this helpful
Try to be simple while explaining simple ideas..
1) add list1 elements to hash.
2) iterate through list2. check if the item is present in hash and add to output list.
3) return output list.
Best way is to leave it here and ask politely if he wants to code this simple naive idea?
Over explaining simple things will cast impression of small knowledge.
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up
1 of 1 people found this helpful
by Harry:
http://codercareer.blogspot.com/2011/11/no-24-intersection-of-sorted-arrays.html