Assign Pins to Shortest Columns
Company: Pinterest
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of greedy assignment strategies and data structures for maintaining and updating minimums in an online allocation problem, while also testing load-balancing reasoning and analysis of time and space complexity.
Constraints
- 1 <= numColumns <= 2 * 10^5
- 0 <= len(heights) <= 2 * 10^5
- 0 <= heights[i] <= 10^9
Examples
Input: ([5, 2, 4, 7, 1], 3)
Expected Output: [5, 9, 5]
Explanation: Pins are assigned in order to the current shortest column: 5->0, 2->1, 4->2, 7->1, 1->2. Final heights are [5, 9, 5].
Input: ([], 4)
Expected Output: [0, 0, 0, 0]
Explanation: With no pins to place, all columns remain at height 0.
Input: ([3, 1], 5)
Expected Output: [3, 1, 0, 0, 0]
Explanation: The first pin goes to column 0, the second to column 1, and the remaining columns stay unused.
Input: ([2, 2, 2, 2], 2)
Expected Output: [4, 4]
Explanation: After two pins the columns are tied at [2, 2]. The third pin goes to column 0 because of the smaller index tie-break, and the fourth goes to column 1.
Input: ([1, 3, 2], 1)
Expected Output: [6]
Explanation: With only one column, every pin must go into that column, so the total is 1 + 3 + 2 = 6.
Input: ([0, 5, 0, 2], 2)
Expected Output: [5, 2]
Explanation: Zero-height pins still follow the same shortest-column rule and can preserve ties. The final column totals are [5, 2].
Hints
- You need to repeatedly find the column with the smallest current total height, breaking ties by smaller index.
- A min-heap storing pairs like `(currentHeight, columnIndex)` lets you update the shortest column efficiently.