Data Structures & AlgorithmsHardProduct Company SDE · Amazon/Microsoft-style OA

Problem: Seat numbers must become pairwise distinct; one move increments a seat number by 1. Find the minimum moves (sort, then push each value just above its predecessor).

Problem: Seat numbers must become pairwise distinct; one move increments a seat number by 1. Find the minimum moves (sort, then push each value just above its predecessor).
def min_incr(a):
    a = sorted(a); moves = 0
    for i in range(1, len(a)):
        if a[i] <= a[i - 1]:
            moves += a[i - 1] + 1 - a[i]
            a[i] = a[i - 1] + 1
    return moves
print(min_incr([4, 1, 4, 2, 2, 9]), min_incr([6, 6, 6]))
What is printed?
  1. A. 3 3
  2. B. 4 3
  3. C. 2 2
  4. D. 2 3

Correct answer: D. 2 3

After sorting, the cheapest distinct assignment keeps the order and raises each value to max(itself, previous + 1). [1, 2, 2, 4, 4, 9] → [1, 2, 3, 4, 5, 9]: 1 + 1 = 2 moves. [6, 6, 6] → [6, 7, 8]: 0 + 1 + 2 = 3. Counting duplicates (2 here) only works when the next value up happens to be free. O(n log n).

More Coding Interview Problems questions

Exams that ask this topic