Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon/Microsoft-style OA

Problem: Burst balloons; bursting balloon i earns left × a[i] × right with its current neighbours (1 beyond the ends). Maximise coins.

Problem: Burst balloons; bursting balloon i earns left × a[i] × right with its current neighbours (1 beyond the ends). Maximise coins.
from functools import lru_cache
def burst(a):
    v = (1,) + a + (1,)
    @lru_cache(None)
    def go(l, r):
        return max((go(l, k) + v[l] * v[k] * v[r] + go(k, r) for k in range(l + 1, r)), default=0)
    return go(0, len(v) - 1)
print(burst((3, 1, 5, 8)), burst((1, 5)))
What is printed?
  1. A. 160 10
  2. B. 167 6
  3. C. 152 10
  4. D. 167 10

Correct answer: D. 167 10

Think of k as the LAST balloon burst between l and r: at that moment its neighbours are exactly v[l] and v[r], so the two sides are independent. Order 1, 5, 3, 8 gives 15 + 120 + 24 + 8 = 167. Choosing the first balloon instead breaks independence. O(n³).

More Coding Interview Problems questions

Exams that ask this topic