Google Interview Question
1,069 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:
Given two linked lists, return the intersection of the two lists: i.e. return a list containing only the elements that occur in both of the input lists.
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (3)
0 of 1 people found this helpful
Your solution is correct though the complexity analysis is incorrect.
Insertion and lookup from the hashtable can take a worst case performance of O(n) or O(logn) if you use a priority queue as the backend storage. This means that while traversing the lists for n times, each time you can encounter a O(n) complexity thus resulting in a worst case performance of O(n^2), or at least O(nlogn).
Helpful Answer?
Yes |
No
Inappropriate?
The actual implementation of a hash table is platform specific. For e.g. C++ hashtables are Balanced binary search trees?
Helpful Answer?
Yes |
No
Inappropriate?
Members can
answer or comment on this question
–
Join Now (It's Free) or
Sign In
1 of 1 people found this helpful
by Radu Litiu:
Traverse the second list. For each element in the list, look it up in the hash table. If it is in the hash table, then insert it into the destination list. Complexity: n * O(1) = O(n)
Total complexity: O(n)