Data Structures & AlgorithmsHard
Find the duplicate in 2 5 9 6 9 3 8 9 7 1 (values 1 to 9, one repeated) with Floyd's method: slow = a[slow] and fast = a[a[fast]], both starting at a[0]. Count the total number of loop iterations across both phases: phase 1 runs until the two pointers meet, and phase 2 runs until the two pointers meet again at the duplicate.
- A. 7
- B. 1
- C. 14
- D. 8
Correct answer: D. 8
Phase 1 (slow one step, fast two) meets after 7 iterations; phase 2 restarts slow at a[0] and the pointers meet at the duplicate 9 after 1 more iteration, total 8. Counting only phase 1 gives 7, only phase 2 gives 1, and 14 counts the fast pointer's moves in phase 1.
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
- What is the largest difference between two CONSECUTIVE values after sorting 3 6 9 1 2 15 in ascending order?
- Selection sort (ascending) is run on 7 2 9 4 1 6, swapping the minimum of the unsorted part into place in each pass, and skipping the swap…
- Sort 7 8 3 12 5 6 15 4 by the NUMBER OF 1-BITS in binary, ascending; values with the same bit count are ordered by value ascending. Give…
- A queue holds 1 2 3 4 5 6 7 8 from front to back. Interleave it: take the front element of the first half, then the front of the second…
- Starting from an empty stack, perform in order: push 4, push 9, pop, push 2, push 7, pop, pop, push 5. Reading from the bottom to the top…
- Take the values of 4 9 2 7 3 8 5 6 1 10 at the ODD 0-based indices (1, 3, 5, …) in order, forming a new sequence. What is the length of…