Largest Square in Histogram

Quick Overview

Given the bar heights of a histogram with unit-width bars, find the area of the largest square that fits on a run of consecutive bars. It tests whether you can adapt histogram rectangle reasoning to a square constraint, handle equal heights and the end of the array, and reach linear time on large inputs.

Largest Square in Histogram

Company: Capital One

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an array of integers `heights` representing a histogram's bar heights where the width of each bar is `1`, return the area of the largest square that can be formed inside the histogram. A square of side length `s` can be formed using `s` **consecutive bars** if each of those bars has a height of at least `s`. The area of such a square is `s * s`. ### Function Signature ```python def largest_square(heights: list[int]) -> int: ``` ### Rules - Side lengths are whole numbers. A square of side `s` covers exactly `s` adjacent bars, and every one of them must be at least `s` tall. - A single bar with a positive height holds a square of side 1, however tall the bar is, because it is only one unit wide. - If every bar has height 0, no square fits and the answer is 0. - Return the area `s * s`, not the side length. ### Constraints - `1 <= heights.length <= 10^5` - `0 <= heights[i] <= 10^4` ### Examples ```text Input: heights = [2,1,5,6,2,3] Output: 4 ``` Bars at indices 2 and 3 have heights 5 and 6, so together they hold a 2 by 2 square. No three consecutive bars are all at least 3 tall, so side 3 is impossible and the area is 4. ```text Input: heights = [4,4,4,4] Output: 16 ``` All four bars are 4 tall and the run is 4 bars wide, so a 4 by 4 square fits. ```text Input: heights = [7,0,3,3,3,2] Output: 9 ``` The bar of height 7 is one unit wide and its neighbor is 0, so it only holds a side 1 square. Bars 2 through 4 are all 3 tall and give a 3 by 3 square; adding bar 5 widens the run to 4 but drops its height to 2.

Overview: Given the bar heights of a histogram with unit-width bars, find the area of the largest square that fits on a run of consecutive bars. It tests whether you can adapt histogram rectangle reasoning to a square constraint, handle equal heights and the end of the array, and reach linear time on large inputs.

Capital One logo
Capital One
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an array of integers heights representing a histogram's bar heights where the width of each bar is 1, return the area of the largest square that can be formed inside the histogram.

A square of side length s can be formed using s consecutive bars if each of those bars has a height of at least s. The area of such a square is s * s.

Function Signature

def largest_square(heights: list[int]) -> int:

Rules

  • Side lengths are whole numbers. A square of side s covers exactly s adjacent bars, and every one of them must be at least s tall.
  • A single bar with a positive height holds a square of side 1, however tall the bar is, because it is only one unit wide.
  • If every bar has height 0, no square fits and the answer is 0.
  • Return the area s * s , not the side length.

Constraints

  • 1 <= heights.length <= 10^5
  • 0 <= heights[i] <= 10^4

Examples

Input: heights = [2,1,5,6,2,3]
Output: 4

Bars at indices 2 and 3 have heights 5 and 6, so together they hold a 2 by 2 square. No three consecutive bars are all at least 3 tall, so side 3 is impossible and the area is 4.

Input: heights = [4,4,4,4]
Output: 16

All four bars are 4 tall and the run is 4 bars wide, so a 4 by 4 square fits.

Input: heights = [7,0,3,3,3,2]
Output: 9

The bar of height 7 is one unit wide and its neighbor is 0, so it only holds a side 1 square. Bars 2 through 4 are all 3 tall and give a 3 by 3 square; adding bar 5 widens the run to 4 but drops its height to 2.