Place Pins Into a Masonry Grid by Shortest Column

Last updated: November 29, 2026

Quick Overview

Pins of varying heights arrive in ranked order and must be placed into a fixed number of equal-width columns, each Pin going to the currently shortest column (leftmost on ties). Return the column for each Pin and the final column heights.

Pinterest
Coding & Algorithms
Frontend Engineer
Pinterest
November 29, 2026
Frontend Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Pins of varying heights arrive in ranked order and must be placed into a fixed number of equal-width columns, each Pin going to the currently shortest column (leftmost on ties). Return the column for each Pin and the final column heights.

The masonry grid is Pinterest's signature layout, and frontend phone screens often ask candidates to implement its core placement rule. The interviewer looks for a min-heap or a simple scan, clean tie-breaking, and awareness of how the layout behaves with infinite scroll and window resizes.

What the Interviewer Expects
  • Chooses the shortest column for each Pin, leftmost on ties
  • Uses a heap keyed by (height, column index) or justifies a linear scan for small column counts
  • Preserves ranked order of Pins rather than reordering for balance
  • Returns both the assignment and final heights
  • Discusses incremental placement for infinite scroll
Key Topics to Cover
heaps
simulation
greedy placement
masonry layout
frontend performance
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you relayout efficiently when the viewport changes from 5 to 3 columns?
  • What if Pin heights are unknown until images load?
  • How would you virtualize the grid so only visible Pins are in the DOM?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: list of Pin heights in rank order, column count >= 1.
  • Each Pin goes to the column with the smallest current height; ties go to the smallest column index.
  • Output: list of column indices an...
High-Level Design

Keep a min-heap of (current_height, column_index). For each Pin, pop the shortest column, record it, and push it back with its new height. Tuple ordering in the heap gives the leftmost tie-break. With...


Submit Your Answer
Markdown supported