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?- A. 11
- B. 12
- C. 9
- 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
- Problem: Use bisect to count how many readings lie in the closed range [2, 7]. python from bisect import bisectleft, bisectright a = [1…
- Problem: Vertical order traversal: group nodes by column (left child c − 1, right child c + 1); inside a column sort by row, then by…
- Problem: Zigzag level-order traversal (left to right, then right to left, alternating). python class T: def init(s, v): s.v, s.l, s.r = v…
- Find the bug: this BST validator compares each node only with its own children. python class T: def init(s, v): s.v, s.l, s.r = v, None…
- A binary tree with 12 nodes is serialised in preorder, writing '#' for every missing child. How many '#' markers appear?
- Problem: Return the diameter of a binary tree (number of EDGES on the longest path between any two nodes). python class T: def init(s, v)…