DSA interview question
You're designing the finale sequence for a fireworks show controlled by a game studio's physics engine. There are n balloons lined up in a row, each painted with a number representing its "coin value." When you burst a balloon, you collect left * current * right coins, where left and right are the values of the adjacent balloons at the time of bursting (not their original neighbors — once a balloon is burst, its former neighbors become adjacent to each other). If there's no balloon on one side (off the boundary), treat that side's value as 1. Your job: figure out the order to burst all balloons in to maximize total coins collected.
Example: nums = [3,1,5,8] — bursting in the right order gives 167 coins, achieved by bursting 1, then 5, then 3, then 8.
int maxCoins(vector<int>& nums);
Input nums | Output | Explanation |
|---|---|---|
[3,1,5,8] | 167 | Burst order: 1→5→3→8 gives 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 15+120+24+8=167 |
[1,5] | 10 | Burst 1 first: 1*1*5=5, then burst 5: 1*5*1=5 → total 10 |
[] | 0 | No balloons, no coins |
[7] | 7 | Single balloon: 1*7*1 = 7 |
[3,1,5] | 35 | Optimal order yields 35 |
(Constraints typically: n == nums.length, 0 <= n <= 300, 0 <= nums[i] <= 100)
nums contain 0s? Does that change strategy? (Doesn't change the algorithm, but worth confirming since a 0-value balloon contributes nothing when burst directly but could still matter as a multiplier if burst at the wrong time — actually since it's a multiplier either way, 0s are handled naturally by the DP.)n up to 300 suggests O(n³) DP is intended (300³ = 27M, feasible) rather than something more exotic.On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in