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?- A. 160 10
- B. 167 6
- C. 152 10
- 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
- Problem: Find the minimum number of intervals to remove so that the rest are non-overlapping – implemented with sort by START and keeping…
- Problem: Find the length of the longest valid (well-formed) parentheses substring. python def longestvalid(s): st, best = [-1], 0 for i…
- Problem: Return the number of provinces (connected components) from an adjacency matrix using DFS. java static int provinces(int[][] m) {…
- Problem: Find the town judge: trusted by everyone else and trusting nobody (−1 if none). python def judge(n, trust): score = [0] (n + 1)…
- Problem: Find the cheapest network latency to reach all nodes from node k (network delay = max shortest distance). python import heapq def…
- Problem: Clone check – count how many nodes a BFS reaches from node 0 in a directed graph that contains a cycle. python from collections…