Yahoo Interview Question
179 Interview Reviews |
Back to all Yahoo Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Engineer at Yahoo:
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (2)
1 of 1 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
Members can
answer or comment on this question
–
Join Now (It's Free) or
Sign In
0 of 0 people found this helpful
by neakor:
A better approach would be to use a separate hash map data structure and iterate the two lists separately like the following:
final Map<Integer, Integer> map = new HashMap<Integer, Integer>();
final int s1 = a1.length;
for(int i = 0; i < s1; i++) map.put(a1[i], a1[i]);
final int s2 = a2.length;
for(int i = 0; i < s2; i++) {
final int v = a2[i];
if(map.containsKey(v)) return v;
}
throw new RuntimeException("No common value found.");
The above is just to show the algorithm, replace integer with node objects. This will allow runtime of O(n) time assuming the values hash key is well spread. but of course, you will end up with O(n) memory consumption.