Data Structures & AlgorithmsHardGATE CS · TCS NQT · Product Company SDE
A singly linked list has nodes at positions 0..6 holding 58 -> 20 -> 49 -> 17 -> 78 -> 37 -> 22, but the last node's next pointer points back to the node at position 0 (a cycle), instead of None. Using Floyd's cycle detection (slow moves 1 step, fast moves 2 steps), what happens?
- A. The program crashes immediately with a null pointer dereference on the first step
- B. Fast pointer reaches None first, proving there is no cycle
- C. Slow and fast pointers never meet because fast always stays exactly one node ahead
- D. Slow and fast pointers eventually meet inside the cycle, proving a cycle exists
Correct answer: D. Slow and fast pointers eventually meet inside the cycle, proving a cycle exists
Since the list has a cycle, the fast pointer can never reach None; because fast closes the gap to slow by one node per step inside the cycle, they are guaranteed to meet.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Heap sort (build a max-heap, then repeatedly swap the root with the last unsorted element and sift down) is run on [1, 27, 40, 40, 63, 2…
- A monotonic decreasing stack of indices is used to compute the next-greater-element array for [41, 35, 17, 34, 4, 37, 29, 44] (push index…
- What is the space complexity of this recursive factorial function, as a function of n (ignoring the final integer result size)? c int…
- Evaluate the postfix expression: 6 3 + 7 + 9 +
- Evaluate the postfix expression: 1 1 - 5 - 5 -
- Heap sort (build a max-heap, then repeatedly swap the root with the last unsorted element and sift down) is run on [91, 96, 8, 20, 80, 10…