Microsoft Interview Question
Software Engineer / DevelopersUse hashing for each node you traverse keep incrementing the node count. The moment the new node clashes in the hash table, you stop traversing and return the count. This has the execution time complexity of O(N) and the space complexity of O(N)
Ravi. You approach only works when the shape is like 6 or 0.
It doesn't work for cases when the shape is like 8 or @.
However, I don't know if 8 or @ are valid cases. Or else, maintaining a hash for pointers will do.
Nil
I believe if you straighten out @ then it will become a 6.. As far as 8 is concerned that's as good as 0
You should not require to split the list. Although approach is same.
1) Find the intersection point. Say P1 is pointing to this node.
2) Start from the head till P1, count the nodes. let say it is P2.
3) Increment the P2 by 1.
4) Again run a loop till P2->next = P1. And keep on updating the count.
1. find where the slow and fast pointers meet.
- googler August 10, 20102. split the LL at that point. So that the loop is removed and now the "6" becomes a "Y" (Think about it and convince yourself that it infact becomes a "Y"
3. Now it becomes a question of finding intersection point of the two LL's .
4. Once you find the intersecting point. Traverse both lists once each.
5. Length of the original LL = The uncommon nodes (from both the LL's) + common nodes counted only once (the tail of the "Y")
6. Join the split ends back together to get a "6" .