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))- A. 4 23
- B. 3 30
- C. 5 30
- 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
- What is printed? python import heapq def huff(freqs): h = list(freqs) heapq.heapify(h) cost = 0 while len(h) > 1: a = heapq.heappop(h) b =…
- What is printed? python def zeros(n): c = 0 while n: n //= 5 c += n return c print(zeros(100), zeros(25))
- What is printed? python def mincut(s): n = len(s) pal = [[False] n for in range(n)] for i in range(n - 1, -1, -1): for j in range(i, n)…
- What is printed? python def triecount(words, pref): root = {} for w in words: node = root for ch in w: node = node.setdefault(ch, {})…
- What is printed? python M = 109 + 7 N = 10 fact = [1] (N + 1) for i in range(1, N + 1): fact[i] = fact[i - 1] i % M def nCr(n, r): return…
- What is printed? python def summin(a): MOD = 109 + 7 st = [] total = 0 cur = 0 for x in a: cnt = 1 while st and st[-1][0] >= x: v, c =…