Goldman Sachs interview question

Design an efficient algorithm that checks if a LinkedList, that holds a trillion elements, is circular or not.

Interview Answers

Anonymous

4 Apr 2016

The stack overflow answer, Tortoise and hare algorithm, of having a fast and slow node traversal is best as it's O(n). The other answers doesn't make sense because if you knew what the last node is, then you've already proven if it's circular or not. Without the benefit of google, I think logically, checking addresses or setting some sort of checked flag for every single node are both acceptable answers as they are O(n) but not as memory efficient O(1) vs. O(n). Personally, it would be very suspicious if someone tries to make it as if they came up with Floyd's algo right on the spot as there are math proofs that needs to be done. Hard to say which is the better answer, since the googled answer just proves that you're good at memorizing stuff vs. the more inefficient answer but that proves you understand performance and linked lists.

Anonymous

7 Feb 2017

I feel like this is very simple, but my solution is expensive memory-wise. Simply make use of a hashset and add elements as you iterate through the list. If you have a hash collision, there is a loop.

Anonymous

15 Oct 2014

Make two pointers that will through the array the first one jumping one step and the second two steps each loop. If they ever meet again that means it's circular.

4

Anonymous

14 Aug 2015

Check the address of the head node and the node that the last node points to , if same then it is a circular linked list.

Anonymous

20 Nov 2014

I guess the question would be trivial if the head and the tail are known, right? If tail.next == head then you would be good, right?

2