ProgrammingVery hardAmazon OA · Flipkart coding round

What is printed?

What is printed?
def min_k(piles, h):
    lo, hi = 1, max(piles)
    while lo < hi:
        mid = (lo + hi) // 2
        if sum(-(-p // mid) for p in piles) <= h:
            hi = mid
        else:
            lo = mid + 1
    return lo
print(min_k([3, 6, 7, 11], 8), min_k([30, 11, 23, 4, 20], 5))
  1. A. 4 23
  2. B. 3 30
  3. C. 5 30
  4. D. 4 30

Correct answer: D. 4 30

Binary search on the speed: a speed is feasible if the total of ceil(pile / speed) fits in the hours. Speed 4 is the smallest that works for the first list, and 30 is needed for five piles in five hours.

More Python Programming questions

Exams that ask this topic