Assign Pins to the Shortest Column
Reported by candidates from Pinterest's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Pinterest OA, reported in May 2026, is scanning all k columns for every pin. With up to 2 * 10^5 pins and k as large as the pin count, that's a quiet O(n*k) blowup that times out on the big cases. The task is simple: drop each pin on the shortest column, break ties by lowest index, return the assignments plus final heights. It's a heap problem wearing a simulation costume. If you blank when the clock is running, StealthCoder sits invisibly on your screen as a safety net and hands you the structure.
The problem
You are given an integer array pins, where pins[i] is the height of the ith pin, and an integer k representing the number of columns. Every column starts with total height 0. Process the pins in their original order. Append each pin to the column with the smallest current total height. If several columns have the same smallest height, choose the one with the smallest index. After a pin is appended, add its height to that column's total height. Return a two-row long[][] matrix: The first row contains the assigned column index for every pin. The second row contains the final total height of every column. Function assignPinsToColumns(pins: int[], k: int) → long[][] Examples Example 1 pins = [1,2,3,4,5] k = 2 return = [[0,1,0,1,0],[9,6]] Both columns begin at height 0. The pins go to columns 0, 1, 0, 1, 0 in order. Their final heights are 1 + 3 + 5 = 9 and 2 + 4 = 6. Example 2 pins = [10,20,30] k = 3 return = [[0,1,2],[10,20,30]] All columns start tied, so the smallest-index rule assigns the three pins to columns 0, 1, and 2. The final heights are 10, 20, and 30. Example 3 pins = [5,5,5,5,5,5] k = 2 return = [[0,1,0,1,0,1],[15,15]] The columns repeatedly tie after every pair of pins. The smallest-index rule therefore gives each new pair to columns 0 and 1 in that order. Both columns finish at height 15. Constraints 1 <= k <= pins.length 1 <= pins.length <= 2 * 10^5 1 <= pins[i] <= 10^9
Reported by candidates. Source: FastPrep
Pattern and pitfall
Use a min-heap keyed on (totalHeight, columnIndex). Seed it with all k columns at height 0. For each pin, pop the top, record its column index in row one, add the pin height to the total, and push it back. Tuple ordering handles the tie rule for free, since equal heights fall back to the smaller index. After processing, fill row two from the final totals per column. Total cost is O((n + k) log k). Two pitfalls. First, heights reach 2 * 10^5 * 10^9, so you need long, not int, or you overflow silently and fail hidden tests. Second, don't forget the index in the heap key, or ties break arbitrarily and example 3 fails. If you freeze on the heap comparator in the live OA, StealthCoder is the hedge that gives you the working version fast.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Assign Pins to the Shortest Column 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
You've seen the question.
Make sure you actually pass Pinterest's OA.
Pinterest 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.
Assign Pins to the Shortest Column FAQ
What's the trick in Assign Pins to the Shortest Column?+
Use a min-heap of (height, index) pairs. Pop the smallest, assign the pin, add its height, push it back. The tuple comparison gives you the smallest-index tie-break automatically. That drops each step from O(k) to O(log k), which the 2 * 10^5 constraint demands.
Why does a brute-force scan fail here?+
Scanning k columns per pin is O(n*k). With n and k both near 2 * 10^5, that's around 4 * 10^10 operations in the worst case. It passes the examples and small tests, then times out on the large hidden ones. The heap fixes it.
Do I need long for the heights?+
Yes. One column can accumulate up to 2 * 10^5 pins of 10^9 each, about 2 * 10^14. That overflows a 32-bit int. The return type is long[][] for exactly this reason, so use long for the totals in your heap too.
How do I handle ties between columns?+
Key the heap on height first, then column index. When heights match, the smaller index pops first. In example 2, all three columns start at 0, so pins go to 0, 1, 2 in order. Forget the index and your output order can drift.
How do I prepare for this in 48 hours?+
Write a clean heap-based simulation once in your language of choice. Know how to push tuples and how the comparator orders them. Then test with example 3, the all-ties case, and a k equals 1 case. That covers nearly every way this fails.