Data Structures & AlgorithmsHardGATE CS · TCS NQT · Product Company SDE
A singly linked list has nodes at positions 0..5 holding 38 -> 23 -> 89 -> 67 -> 63 -> 79, but the last node's next pointer points back to the node at position 1 (a cycle), instead of None. Using Floyd's cycle detection (slow moves 1 step, fast moves 2 steps), what happens?
- A. Slow and fast pointers never meet because fast always stays exactly one node ahead
- B. Slow and fast pointers eventually meet inside the cycle, proving a cycle exists
- C. The program crashes immediately with a null pointer dereference on the first step
- D. Fast pointer reaches None first, proving there is no cycle
Correct answer: B. 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
- Two nested loops iterate: the outer runs n times, and for each outer iteration the inner loop runs from 1 up to the current outer index i…
- What is the time complexity of binary searching for a value in a sorted array of n elements, then linearly scanning the next k elements…
- What does this code compute, and what is its time complexity for an array of n elements? c int best = a[0]; for (int i = 1; i < n; i++) if…
- Quicksort uses the Lomuto partition with the last element as pivot on [99, 24, 44, 86, 76, 60, 84, 3]. After one partition call, at which…
- What is the time complexity of this code, as a function of n? c for (int i = 0; i < n; i++) for (int j = i; j < n; j++) sum += a[i] a[j];
- A sorted array is [1, 9, 9, 10, 13, 14, 18, 23, 23, 25]. Using the standard two-pointer in-place duplicate-removal technique (a slow…