Data Structures & AlgorithmsHardAmazon · Microsoft OA

Problem: Find the smallest divisor d such that Σ ceil(a[i] / d) ≤ threshold.

Problem: Find the smallest divisor d such that Σ ceil(a[i] / d) ≤ threshold.
def smallest_div(a, t):
    lo, hi = 1, max(a)
    while lo < hi:
        d = (lo + hi) // 2
        if sum((x + d - 1) // d for x in a) <= t: hi = d
        else: lo = d + 1
    return lo
print(smallest_div([7, 17, 3, 25], 8))
What is printed?
  1. A. 9
  2. B. 8
  3. C. 10
  4. D. 13

Correct answer: A. 9

d = 8 gives 1 + 3 + 1 + 4 = 9 > 8, while d = 9 gives 1 + 2 + 1 + 3 = 7 ≤ 8, and the sum only decreases as d grows, so binary search returns 9. Using floor division instead of ceiling would wrongly accept smaller divisors.

More Coding Interview Problems questions

Exams that ask this topic