Data Structures & AlgorithmsVery hardProduct company OA
Problem: Flower i blooms on day bloom[i]. A bouquet needs k ADJACENT bloomed flowers, each flower used once. Find the minimum day by which m bouquets can be made (−1 if impossible), binary searching on the day.
Problem: Flower i blooms on day bloom[i]. A bouquet needs k ADJACENT bloomed flowers, each flower used once. Find the minimum day by which m bouquets can be made (−1 if impossible), binary searching on the day.
def min_days(bloom, m, k):
if m * k > len(bloom): return -1
def ok(day):
run = made = 0
for b in bloom:
run = run + 1 if b <= day else 0
if run == k: made += 1; run = 0
return made >= m
lo, hi = min(bloom), max(bloom)
while lo < hi:
mid = (lo + hi) // 2
if ok(mid): hi = mid
else: lo = mid + 1
return lo
b = [2, 9, 4, 3, 8, 1, 7]
print(min_days(b, 2, 2), min_days(b, 3, 2), min_days(b, 4, 2))What is printed?- A. 7 9 -1
- B. 4 9 -1
- C. 7 8 -1
- D. 7 9 9
Correct answer: A. 7 9 -1
Feasibility is monotone in the day, and ok(day) greedily cuts runs of bloomed flowers into bouquets. Day 7: bloomed at positions 0, 2, 3, 5, 6 → pairs (2, 3) and (5, 6) → 2 bouquets (day 4 gives only one). Three bouquets need day 9: on day 8, positions 2–6 give only two pairs. Four bouquets need 8 > 7 flowers → −1. O(n log(max day)).
More Coding Interview Problems questions
- Problem: A number is 'magical' if it is divisible by a or b. Find the n-th magical number by binary search with inclusion–exclusion…
- Problem: Find the maximum difference between successive values in the SORTED order of an array, in linear time, using buckets (pigeonhole…
- Problem: Camera clips (start, end) cover parts of an event lasting T = 10 minutes; clips may be trimmed. Find the minimum number of clips…
- Problem: A broken calculator shows start and supports only 'double' and 'decrement by 1'. Find the minimum operations to reach target…
- Problem: Return the largest number ≤ n whose digits are non-decreasing from left to right. python def mono(n): d = list(str(n)); mark =…
- Problem: A string has '(', ')' and '', where '' may act as '(', ')' or nothing. Is it balanceable? Track the range [lo, hi] of possible…