Data Structures & AlgorithmsVery hardGoogle · Flipkart interview
A 'shuffle' swaps each position i with a position chosen uniformly from the WHOLE array (0..n−1), for i = 0..n−1. Why is this not a uniform shuffle?
- A. Because swapping an element with itself is wasted work
- B. Its nⁿ equally likely paths cannot split evenly among the n! permutations when n ≥ 3
- C. Because the last element is never moved
- D. It is uniform; it is merely slower than Fisher–Yates
Correct answer: B. Its nⁿ equally likely paths cannot split evenly among the n! permutations when n ≥ 3
Each of the nⁿ random choice sequences is equally likely and produces some permutation; a uniform result would need every permutation to get the same number of sequences, i.e. n! | nⁿ, which fails for n = 3 (27 vs 6). Fisher–Yates picks j from i..n−1, giving exactly n! paths.
More Coding Interview Problems questions
- What is the time complexity of this loop? python for i in range(1, n + 1): j = i while j <= n: work() # O(1) j += i
- Problem: Maximum product of a contiguous subarray (values may be negative or zero). python def maxprod(a): hi = lo = best = a[0] for x in…
- Problem: Taking a value v earns v points for EVERY copy of v, but then all copies of v − 1 and v + 1 are deleted. Maximise the points…
- Problem: Travel on given days; passes cost costs[0] (1 day), costs[1] (7 days) and costs[2] (30 days). Minimise the total cost. python def…
- Problem: Houses stand in a CIRCLE (the first and last are neighbours); adjacent houses cannot both be robbed. python def robline(a): take…
- Problem: Each person is [h, k]: height h, and exactly k people in front are at least as tall. Rebuild the queue (sort tallest first…