Skip to the solution
HardDSA11 min read

Burst Balloons: why does DP over intervals (not balloons) work?

580 views

The problem

Question presented to candidate: "You have n balloons in a row, each with a number. Bursting balloon i earns left × nums[i] × right, where left and right are its current neighbours (a missing neighbour counts as 1). Find the maximum coins. Code it in Python, and explain why the DP is over intervals and why you choose the balloon burst last rather than first."

What a strong answer should cover:

  • Why "choose the first balloon" fails: its neighbours become adjacent, so the two sides are not independent sub-problems.
  • The insight: choose the balloon k burst last in the open interval (i, j); it separates the two sides, and its neighbours are fixed as a[i] and a[j].
  • Padding with 1s, the state dp[i][j], the recurrence max(dp[i][k] + a[i]·a[k]·a[j] + dp[k][j]), and filling by interval length.
  • O(n³) time and O(n²) space versus O(n · n!) for trying every order.
  • Python details: build the 2-D table with a comprehension (never [[0] * n] * n), pad with [1] + nums + [1], and know that a top-down @lru_cache version is short but pays recursion and call overhead; ints never overflow.

Clarifying questions expected:

  • What does a missing neighbour count as, and how large are n and the values?
  • Do you need the burst order, or only the maximum?

Code / implementation expected: Yes — max_coins(nums) in Python with a padded list and an O(n³) interval table.

  • dp
  • interval
  • Google

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

11 min read · 14 sections

Python code

Edit the code and run it here. Each printed line with an expected value becomes a test.

PythonPop-last interval DP with the order of pops and the dp table
PythonBrute force vs broken pop-first split vs pop-last DP

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 146 of 154 in DSA

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All DSA questions
Up nextHard

How do you solve the N-Queens problem?

Continue