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?- A. 9
- B. 8
- C. 10
- 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
- Problem: Buses i take time[i] minutes per trip and run independently and repeatedly. Find the minimum time for all buses together to…
- 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?