Data Structures & AlgorithmsVery hardGATE CS
An array next = [10, 6, 10, 1, 1, 9, 5, 5, 10, 3, 7] defines a linked structure in which the node after index i is next[i]. Floyd's algorithm starts both pointers at index 0, moves slow by one step and fast by two steps per round, and stops when they meet. At which index do they meet?
- A. 9
- B. 3
- C. 1
- D. 5
Correct answer: B. 3
The path from 0 enters a cycle at index 5 (cycle length 5). Simulating slow and fast pointers gives their first meeting at index 3; the cycle entry 5 is found only in the second phase.
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 sorted lists A = [2, 4, 4, 7, 9, 12, 15, 18, 21] and B = [1, 4, 5, 6, 13, 14, 16, 17] are merged with the standard merge procedure…
- Insertion sort (stable, scanning leftwards while a[j] > key) sorts [6, 3, 9, 3, 1, 8, 2, 7, 5, 4]. Counting every evaluation of the…
- For each index i of [4, 7, 3, 8, 2, 2, 9, 1, 6, 5, 10, 3], let g(i) be the index of the first element to the right that is strictly…
- An array of 15 elements is rotated right by 6 positions using three in-place reversals (reverse the whole array, then reverse the first 6…
- What is the value of the postfix expression 5 3 2 + 4 - 6 2 / + (integer division)?
- A circular queue is kept in an array of size 7. The front index is 5 and the queue currently holds 4 elements. At which index will the…