MEDIUMasked at 1 company

Minimum Cost to Connect Sticks

A medium-tier problem at 71% community acceptance, tagged with Array, Greedy, Heap (Priority Queue). Reported in interviews at J.P. Morgan and 0 others.

Founder's read

You have N sticks, each with a cost to connect pairs, and each connection produces a new stick whose cost equals the sum of the two input sticks. You need the minimum total cost to connect all sticks into one. This is a classic greedy heap problem that looks easy until you realize the order matters enormously. J.P. Morgan asks it. The trap is trying to brute-force or sort and forget; the pattern is always combine the two smallest. If you lock up on the greedy insight during your live OA, StealthCoder surfaces the heap solution instantly, invisible to the proctor.

Companies asking
1
Difficulty
MEDIUM
Acceptance
71%

Companies that ask "Minimum Cost to Connect Sticks"

If this hits your live OA

Minimum Cost to Connect Sticks is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop.

Get StealthCoder
What this means

The trick is counter-intuitive if you haven't seen it: always combine the two cheapest sticks next, not the cheapest and largest. Each time you merge two sticks of cost A and B, that resulting stick (cost A+B) will be used again in future merges, multiplying its cost. By keeping intermediate sticks small, you minimize total cost. A min-heap lets you extract the two smallest in O(log N) time per operation, totaling O(N log N). The obvious approach, sorting and greedily pairing, fails because you might create expensive intermediate sticks early. Common mistake: implementing the heap correctly but forgetting that each merge produces a new stick that goes back into the heap. StealthCoder is your safety net if you blank on this pattern and need a working solution fast.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Minimum Cost to Connect Sticks recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Cost to Connect Sticks interview FAQ

Is this problem really asking for minimum cost to merge, not minimum merges?+

Yes. The cost of merging two sticks is their sum, and you accumulate that cost each time you merge. Total cost is the sum of all intermediate stick costs plus the final result. It's not about minimizing the number of operations; it's about the arithmetic sum of all merge events.

Why doesn't sorting sticks once and then pairing adjacent ones work?+

Because sorted pairing ignores future re-use. When you merge two sticks, the result re-enters the pool. By always picking the two smallest available (not just two adjacent in a fixed order), you keep intermediate costs low and compound savings throughout the process.

What data structure do I actually need?+

A min-heap or priority queue. You need to repeatedly extract the two smallest elements and re-insert their sum. Sorting once is O(N log N) but only works once; a heap lets you maintain the minimum dynamically across N operations.

Does J.P. Morgan really ask this one, and how often?+

Yes, J.P. Morgan has asked it. It's not high-frequency across all companies, but it's a reliable medium-difficulty greedy problem that tests heap understanding and algorithmic intuition beyond simple sorting.

What if I implement the heap but forget to re-insert merged sticks?+

You'll get the wrong answer. Each merge produces a new stick that must go back into the heap for future merges. Forgetting that step is a common runtime error that breaks the entire algorithm logic.

Want the actual problem statement? View "Minimum Cost to Connect Sticks" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.