Data Structures & AlgorithmsHardAmazon · Microsoft OA

Problem: Buses i take time[i] minutes per trip and run independently and repeatedly. Find the minimum time for all buses together to complete at least trips trips.

Problem: Buses i take time[i] minutes per trip and run independently and repeatedly. Find the minimum time for all buses together to complete at least `trips` trips.
def min_time(time, trips):
    lo, hi = 1, min(time) * trips
    while lo < hi:
        mid = (lo + hi) // 2
        if sum(mid // t for t in time) >= trips: hi = mid
        else: lo = mid + 1
    return lo
print(min_time([2, 3, 7], 10))
What is printed?
  1. A. 11
  2. B. 12
  3. C. 9
  4. D. 20

Correct answer: B. 12

At t = 11 the buses finish 5 + 3 + 1 = 9 trips; at t = 12 they finish 6 + 4 + 1 = 11 ≥ 10, so 12. Using real division mid / t counts partial trips and gives 11, counting an unfinished trip with a ceiling gives 9, and 20 = min(time) × trips is only the search's upper bound (the fastest bus alone).

More Coding Interview Problems questions

Exams that ask this topic