Minimum Stick Connection Cost
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Strip away the stick story and this Amazon OA question, reported in September 2026, is Huffman coding in disguise. Every connection costs the sum of the two sticks, so the real job is deciding which pairs merge first. If you've got an invite and 48 hours, this is a pattern you can lock in tonight. It's a min-heap problem with a greedy rule, and it's short once you see it. If you blank during the live assessment, StealthCoder sits invisibly on your screen and hands you the solution so one bad minute doesn't sink the attempt.
The problem
You are given an integer array sticks, where each element is the length of a stick. You may connect any two sticks with lengths x and y. The new stick has length x + y, and the cost of this operation is also x + y. Return the minimum total cost required to connect all sticks into one stick. Function minimumStickConnectionCost(sticks: int[]) → int Examples Example 1 sticks = [2, 4, 3] return = 14 Connect 2 and 3 for cost 5, then connect 5 and 4 for cost 9. The total cost is 14. Example 2 sticks = [1, 8, 3, 5] return = 30 One optimal sequence is 1 + 3 = 4, then 4 + 5 = 9, then 8 + 9 = 17, for total cost 4 + 9 + 17 = 30. Constraints 1 <= sticks.length <= 104 1 <= sticks[i] <= 104
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: always merge the two smallest sticks. Small sticks get re-added into later merges, so you want the big ones to be counted as few times as possible. Push all lengths into a min-heap, pop two, add their sum to the total cost, push the sum back, and repeat until one stick remains. That's O(n log n) time and O(n) space. The common pitfall is sorting once and merging left to right. That fails, because the merged stick can be larger than the next element and the order changes. Check example 2 by hand: 1+3=4, 4+5=9, 9+8=17, total 30. Also handle length 1, where the cost is 0 and you never enter the loop. Use a 64-bit-safe accumulator in languages where overflow is a concern. StealthCoder is your hedge in the live OA if the heap loop slips your mind, but the logic here is only about ten lines.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Minimum Stick Connection Cost cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as minimum cost to connect sticks. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Stick Connection Cost FAQ
What's the trick to Minimum Stick Connection Cost?+
Greedy with a min-heap. Always merge the two shortest sticks, add that sum to your total, and push the new stick back. Shorter sticks get re-counted in later merges, so you want the longest ones merged last. Repeat until one stick is left.
How hard is this one really?+
Easy to medium. The code is short, but the greedy insight is what people miss. If you've seen Huffman coding or a similar merge problem, it takes five minutes. If not, you may try sorting once, which gives wrong answers.
Why doesn't sorting once and merging left to right work?+
After a merge, the new stick can be bigger than the next sorted element, so the smallest-two order changes. For [1,8,3,5], a naive pass gives a higher cost than 30. You need a structure that re-inserts and always returns the minimum, which is a heap.
What's the time complexity and what edge cases matter?+
O(n log n) time, O(n) space for the heap. With up to 10^4 sticks that's fine. The edge case is a single stick, where the answer is 0 because no merge happens. Make sure your loop only runs while the heap has at least two items.
How do I prepare for this in 48 hours?+
Write the heap solution from scratch twice, then trace example 1 and example 2 by hand. Then practice one or two related heap-greedy problems so the pattern feels automatic. Know your language's min-heap API cold, since that's where most time gets lost.