Data Structures & AlgorithmsHardAmazon/Microsoft SDE pattern
Floyd's cycle detection runs on a linked list 1 → 2 → ... → 7 whose last node's next pointer points back to node 5 (0-based position 4). Slow moves one step, fast moves two steps per iteration until they meet. At what value do they first meet, and what value starts the cycle?
- A. meet 7, cycle start 6
- B. meet 7, cycle start 5
- C. meet 5, cycle start 5
- D. meet 1, cycle start 5
Correct answer: B. meet 7, cycle start 5
Simulating slow/fast pointer positions until they coincide gives the meeting node with value 7. Resetting one pointer to the head and advancing both one step at a time, they meet again exactly at the cycle's start, value 5 — this works because the head-to-start distance equals the meeting-point-to-start distance modulo the cycle length.
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
- Heights [1, 6, 2, 6, 2, 5, 2, 2, 7] represent vertical lines at each index. What is the maximum water area (min(height) × horizontal…
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…
- Elevation map [4, 5, 4, 4, 0, 1, 2, 1, 0, 2] (bar width 1). Using the two-pointer / prefix-max approach for trapping rain water, how much…
- A circular queue of capacity 4 starts empty and runs this sequence in order: enqueue(3), enqueue(6), enqueue(4), enqueue(7), dequeue()…
- The Boyer–Moore majority vote algorithm (candidate/counter, no verification pass) runs on [7, 5, 2, 5, 5, 5, 2, 7]. What candidate and…
- What does this loop-counting code print? (Python) python n, c = 45, 0 i = 1 while i < n: j = i while j < n: c += 1 j = 2 i += 1 print(c)