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 Software Development Engineer at Yahoo:
None.. All questions were simple. Reverse a linked list, find a duplicate node in the linked list, etc
See more for this Yahoo Software Development Engineer Interview
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (4)
Thus,
Time to iterate linked list = T_LL = O(n)
Time to iterate hash = T_H = O(n)
Time to insert to heap = T_Heap = O(log n)
and Time to access root node (max occurrences in Heap) = T_H_Access = O(1)
Clearly T_LL + T_H > T_Heap + T_H_Access
I am sure more ways exist! This just illustrates the work for 2 ways.
Helpful Answer?
Yes |
No
Inappropriate?
1 of 1 people found this helpful
Reverse the list:
public void reverse(final List<Node> list) {
Node prev = null;
Node next = list.head;
Node n = list.head;
while(next != null) {
next = n.next;
n.next = prev;
prev = n;
n = next;
}
}
as you can see, you don't need a second list. this will run in O(n) time.
Also for finding the duplicate. Just use a hash map. on average you get O(1) for insertion into the map and you don't even need to go through the entire list as the question only asks to find "a" duplicate node. so something like this.
public Node findDuplicate(final List<Node> list) {
final Map<Node, Node> map = new HashMap<Node, Node>();
for(final Node n : list) {
if(map.contains(n)) return n;
else map.put(n, n);
}
}
Worst case run the loop n times each contains check worst case O(n) but average should be O(1). So your best case runtime for the whole thing should be O(n), and worst case O(n^2).
Helpful Answer?
Yes |
No
Inappropriate?
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 Anonymous: