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

Problem: A courier eats through n ≤ 10⁵ piles of parcels, pile sizes up to 10⁹, at speed k per hour (a pile smaller than k still takes a full hour). Find the minimum k to finish within h hours. What is the time complexity of the intended solution?

  1. A. O(M · n) – try every speed from 1 upwards
  2. B. O(n log n) – sort the piles and pick the median
  3. C. O(n log M), where M = the largest pile – binary search k and check each guess in O(n)
  4. D. O(n) – the answer is ceil(sum / h)

Correct answer: C. O(n log M), where M = the largest pile – binary search k and check each guess in O(n)

Hours needed, Σ ceil(pile/k), only decreases as k grows, so binary search k in [1, M] with an O(n) check: about 30 × 10⁵ operations. Trying every speed is up to 10¹⁴ steps. ceil(sum/h) is only a lower bound, because a partial hour cannot be shared between piles.

More Coding Interview Problems questions

Exams that ask this topic