Reported January 2024
Navanheap priority queue

Minimum Stick Connection Cost

Reported by candidates from Navan's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Navan OA. Under 2s to a working solution.
Founder's read

Strip away the stick story and the Navan OA from January 2024 is a Huffman-style merge: always combine the two smallest values, and a min-heap does it for you. If your invite lands in the next day or two, this is a short problem with one real trick and one follow-up that can trip you. The input is an array of up to 10^4 lengths, and the answer is the sum of every merge cost. Know the greedy, know why it works, and you're done in ten minutes. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment.

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.
Interview follow-up
The interviewer also asked what to do when the input cannot fit in memory. Discuss an external-memory implementation of the minimum-cost merge process. The judged function here uses the supplied in-memory array.

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 <= 10^4
1 <= sticks[i] <= 10^4

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: every time you merge, the merged length gets paid again in every later merge it joins. So long sticks should be merged late and short sticks early. Greedy says pop the two smallest from a min-heap, add their sum to the total, push the sum back, and repeat until one stick remains. That's O(n log n). Common pitfalls: sorting once and merging left to right (wrong, since the merged stick may no longer be the smallest), forgetting the single-stick case where the cost is 0, and overflow in other languages, though 10^4 times 10^4 sums can reach large totals, so use a 64-bit type to be safe. Check example 2: 1+3=4, 4+5=9, 8+9=17, total 30. The follow-up asks about data that doesn't fit in memory. Talk through external sorting into runs, then a k-way merge feeding a heap. StealthCoder is the hedge if the heap code slips your mind live.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as minimum cost to connect sticks. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Navan's OA.

Navan reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Stick Connection Cost FAQ

What's the trick in the Navan minimum stick connection cost problem?+

It's greedy with a min-heap. Always merge the two smallest sticks, add their sum to the total, and push the sum back. Small sticks get re-added the most times, so you want them combined first. Stop when one stick is left.

How hard is this OA question really?+

Easy to medium. Once you spot the Huffman-style greedy, the code is about ten lines. The difficulty is recognizing it and not sorting once and walking left to right, which gives wrong answers.

What should I answer for the out-of-memory follow-up?+

Say you'd sort chunks that fit in memory and write them to disk as sorted runs. Then stream them with a k-way merge, keeping only the current smallest candidates in a heap. Mention that the merge costs still come from repeatedly taking the two smallest values.

What edge cases should I test?+

A single stick returns 0 since no merge happens. Two sticks return their sum. Test many equal lengths and the max size of 10^4 sticks at 10^4 length, where the total gets large, so use a wide integer type.

How do I prepare for this in 48 hours?+

Write the heap solution from scratch twice. Trace both examples by hand to confirm 14 and 30. Then rehearse the external-memory explanation out loud in under a minute. That covers both the coding and the discussion part.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Navan.

OA at Navan?
Invisible during screen share
Get it