Data Structures & AlgorithmsHardProduct Company SDE · Amazon/Microsoft-style OA
Problem: Reconstruct a queue of people (h, k) where k = number of people in front with height ≥ h.
Problem: Reconstruct a queue of people (h, k) where k = number of people in front with height ≥ h.
def rebuild(people):
people.sort(key=lambda p: (-p[0], p[1])); q = []
for p in people: q.insert(p[1], p)
return q
print(rebuild([[7, 0], [4, 4], [7, 1], [5, 0], [6, 1], [5, 2]]))What is printed?- A. [[7, 0], [5, 0], [5, 2], [6, 1], [4, 4], [7, 1]]
- B. [[5, 0], [7, 0], [6, 1], [5, 2], [4, 4], [7, 1]]
- C. [[5, 0], [7, 0], [5, 2], [6, 1], [4, 4], [7, 1]]
- D. [[4, 4], [5, 0], [5, 2], [6, 1], [7, 0], [7, 1]]
Correct answer: C. [[5, 0], [7, 0], [5, 2], [6, 1], [4, 4], [7, 1]]
Insert tallest first (ties by k): when a person is inserted at index k, everyone already placed is at least as tall, so exactly k of them are in front; shorter people inserted later do not affect that count. O(n²) with list inserts.
More Coding Interview Problems questions
- Problem: Find the minimum number of refuelling stops to travel a distance, given stations (position, fuel) and start fuel. python import…
- Problem: A printer raises ink levels by choosing any contiguous block of cartridges and adding 1 unit to each in one operation. Starting…
- Problem: Count contiguous subarrays whose max − min ≤ k, using two monotonic deques and a shrinking left edge. python from collections…
- Problem: Return the k-th smallest absolute difference among all pairs of readings (n up to 10⁵, so the n² pairs cannot be listed). Binary…
- Problem: Tasks of given lengths must be packed into work sessions of at most T = 8 hours; a task cannot be split. Exact bitmask DP (state…
- Problem: Paint a 3 × n wall: each cell gets one of 3 colours and side-adjacent cells must differ. Count valid colourings by DP over row…