Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon/Microsoft-style OA
Problem: A linked list of 10 nodes is traversed with Floyd's tortoise and hare: the slow pointer moves 1 step and the fast pointer moves 2 steps each round. The list has a cycle that begins at node index 4 (0-indexed, the tail is nodes 0–3 and the cycle is nodes 4–9 and returns to node 4). After how many rounds do the two pointers first meet?
- A. 4
- B. 10
- C. 12
- D. 6
Correct answer: D. 6
Simulating: after 4 rounds slow enters the cycle at node 4 while fast is at node 8; the gap of 2 closes by 1 per round in a cycle of 6, so they meet after 6 rounds at node 6. 4 is only when slow enters the cycle, 10 is the list length, and 12 counts the fast pointer's steps.
More Coding Interview Problems questions
- Problem: An LRU cache holds 3 entries. Keys are accessed in this order: 1, 2, 3, 1, 4, 2, 5, 1, 3, 4. A key is a hit if it is already…
- Problem: Tower of Hanoi with 6 disks, moving all disks from peg A to peg C using peg B as the spare (standard rules). How many moves does…
- Problem: Count the integers from 1 to 999 whose decimal digit sum is divisible by 7.
- Problem: Compute Euler's totient φ(n) summed over n = 1 to 30 (φ(n) counts integers in [1, n] coprime to n, with φ(1) = 1).
- Problem: How many positive divisors does 720 have?
- Problem: Find the modular inverse of 7 modulo 26 (the value x with 0 ≤ x < 26 such that 7x ≡ 1 mod 26).