Home › Company-wise questions › Microsoft › Burst Balloons ← Microsoft bank
Microsoft · Question 69 · Dynamic Programming
Burst Balloons Dynamic Programming Java code ⏱ O(n³) 💾 O(n²)
Product-based Published and maintained by DSA-topics · Last updated 30 November 2026 · Report an error
About this bank These are classic technical-interview concepts compiled from publicly circulated company-wise question sets, rewritten with original explanations and compiler-ready solutions for educational practice; inclusion does not imply endorsement by the company. Spotted an error? Tell us .
Problem Given an array nums, burst balloons to maximize coins. You get nums[i-1] * nums[i] * nums[i+1] for bursting i.
Approach Interval DP: try each balloon as the last burst in its range.
Time O(n³)Space O(n²)
How it works 1 Pad with virtual 1-balloons. 2 dp[left][right] = best coins strictly inside. 3 Try each balloon as the last burst in the interval. Sample input / output Input:
4
3 1 5 8
Output:
167 Constraints 1 <= n <= 300; 0 <= nums[i] <= 100.
Solution — read it, then walk it Flowchart Pseudocode
Time O(n³)Space O(n²)Loops 5Returns 1source · Java
Scroll sideways for wide charts — the view follows the active step. ⤢ Expand
↻ repeat ↻ repeat ↻ repeat START import java.util.* DEFINE class Main DEFINE static int maxCoins(int[] nums) DEFINE n = nums.length DEFINE [] newNums = new int[n + 2] newNums[0] = newNums[n + 1] = 1 for (int i = 0; i < n; i++) newNums[i + 1] = nums[i] DEFINE [][] dp = new int[n + 2][n + 2] for (int len = 2; len <= n + 1; len++) for (int left = 0; left <= n + 1 - len; left++) DEFINE right = left + len for (int k = left + 1; k < right; k++) dp[left][right] = Math.max(dp[left][right], RETURN dp[0][n + 1] DEFINE static void main(String[] args) java.util.Scanner sc = new java.util.Scanner(System.in) DEFINE n = sc.nextInt() DEFINE [] nums = new int[n] for (int i = 0; i < nums.length; i++) nums[i] = sc.nextInt() System.out.println(maxCoins(nums)) END start / end statement condition loop returns definition← Q68 · Word Search II Q70 · Regular Expression Matching → Solution code Core logic highlighted in green
Language Python Javasource C++ C Core logic Full code Copy
Recommended resources Affiliate links · may earn us a commission
Classic DSA interview books The reference books interviewers themselves learned from — great next to the visualizers. See the books → Structured coding courses Video courses that pair well with visual practice — pick one language and go deep. Browse courses → Cloud hosting for side projects Ship the portfolio project that gets you hired — free credits for new accounts. Claim credits →