Largest Banner That Fits Above a Masonry Column Skyline

Last updated: November 29, 2026

Quick Overview

After laying out a masonry grid, each column has some free vertical space left at the bottom of the viewport. Given those free heights for adjacent equal-width columns, find the largest rectangular banner (area in column-units times pixels) that fits, and which columns it spans.

Pinterest
Coding & Algorithms
Frontend Engineer
Pinterest
November 29, 2026
Frontend Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


After laying out a masonry grid, each column has some free vertical space left at the bottom of the viewport. Given those free heights for adjacent equal-width columns, find the largest rectangular banner (area in column-units times pixels) that fits, and which columns it spans.

Pinterest's uneven masonry columns leave ragged gaps that product teams sometimes want to fill with a module such as a shopping carousel or a prompt. This is the largest rectangle in a histogram in disguise; the interviewer checks the monotonic stack invariant and the width calculation when popping.

What the Interviewer Expects
  • Maps the layout problem to largest rectangle in a histogram
  • Maintains a stack of indices with increasing heights
  • Computes width using the new stack top as the left boundary
  • Uses a sentinel zero at the end to flush the stack
  • Returns the span, not only the area
Key Topics to Cover
monotonic stack
largest rectangle in histogram
layout
arrays
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 require the banner to span at least 2 columns?
  • What if the free space is a 2D grid of booleans instead of column heights?
  • How would you update the answer incrementally as new Pins load into columns?
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
  • heights: non-negative integers, one per adjacent column.
  • The banner is a rectangle spanning a contiguous range of columns with height at most the minimum free height in that range.
  • Output (area,...
High-Level Design

For each column as the limiting (shortest) column, the best rectangle extends left and right until a strictly shorter column. A stack of increasing heights finds both boundaries in one pass: when the ...


Submit Your Answer
Markdown supported