employer cover photo
employer logo
employer logo

Hughes Network Systems

Part of EchoStar

Engaged employer

Hughes Network Systems interview question

How do you Catch Loops in Two Passes

Interview Answer

Anonymous

Jan 5, 2014

Simultaneously go through the list by ones (slow iterator) and by twos (fast iterator). If there is a loop the fast iterator will go around that loop twice as fast as the slow iterator. The fast iterator will lap the slow iterator within a single pass through the cycle. Detecting a loop is then just detecting that the slow iterator has been lapped by the fast iterator. Ref:http://ostermiller.org/find_loop_singly_linked_list.html