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?
  1. A. 7 9 -1
  2. B. 4 9 -1
  3. C. 7 8 -1
  4. 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

Exams that ask this topic