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?
- A. O(M · n) – try every speed from 1 upwards
- B. O(n log n) – sort the piles and pick the median
- C. O(n log M), where M = the largest pile – binary search k and check each guess in O(n)
- 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
- Problem: Place c routers at given positions on a road so the minimum distance between any two routers is as large as possible. python def…
- Problem: The function below should return the integer square root ⌊√x⌋. java static int isqrt(int x) { int lo = 0, hi = x, ans = 0; while…
- Problem: In an array where every element appears twice except one, and pairs sit next to each other, the array is sorted. Find the single…
- Problem: Find a peak element (strictly greater than its neighbours; outside the array counts as −∞). Return its index. python def peak(a)…
- Problem: Return the k-th smallest element of the union of two sorted arrays (k is 1-based) in O(log(m + n)). python def kth(a, b, k): if…
- Problem: Reverse a singly linked list of order IDs. python class Node: def init(self, v, nxt=None): self.v, self.next = v, nxt def…