Data Structures & AlgorithmsHardAmazon · Microsoft OA
Problem: Koko eats from one pile per hour at speed k bananas/hour (if a pile has fewer, she finishes it and waits). Find the minimum k to finish within h hours.
Problem: Koko eats from one pile per hour at speed k bananas/hour (if a pile has fewer, she finishes it and waits). Find the minimum k to finish within h hours.
def koko(piles, h):
lo, hi = 1, max(piles)
while lo < hi:
k = (lo + hi) // 2
if sum((p + k - 1) // k for p in piles) <= h: hi = k
else: lo = k + 1
return lo
print(koko([9, 25, 14, 7], 8))What is printed?- A. 6
- B. 7
- C. 9
- D. 5
Correct answer: C. 9
Speed 9: 1 + 3 + 2 + 1 = 7 hours ≤ 8, while speed 8 needs 2 + 4 + 2 + 1 = 9 hours, so 9. Floor division p // k forgets the partial hour and gives 6, ceil(55 / 8) = 7 assumes piles can be shared across hours, and giving all four spare hours to the biggest pile (ceil(25 / 5)) gives 5 but ignores the pile of 14.
More Coding Interview Problems questions
- Problem: Length of the longest common subsequence of THREE strings (3-D DP). python def lcs3(a, b, c): dp = [[[0] (len(c) + 1) for in…
- Problem: Length of the longest strictly increasing path in a matrix (moves in 4 directions), with memoised DFS. python from functools…
- Problem: Smash stones pairwise (x ≤ y leaves y − x) until at most one remains; minimise the final weight. Equivalent to splitting into two…
- Problem: You may swap a[i] with b[i] at any indices. Find the minimum swaps making BOTH arrays strictly increasing (DP with 'swapped at i'…
- Problem: Remove every interval that is covered by another interval ([a, b] is covered by [c, d] if c ≤ a and b ≤ d); count those that…
- Problem: Delete the fewest characters from a string of a's and b's so that no 'b' comes before an 'a'. python def mindel(s): bseen = dels…