Largest Square in a Histogram
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks most first attempts at the ZipRecruiter largest-square-in-a-histogram question is solving the wrong problem. Candidates reported it in February 2022, and plenty of them reach for the max rectangle area and return that. A square isn't a rectangle, and the answer is capped by the width of the window. If you've got the OA in a day or two, learn the cap and the search idea below. And if you blank mid-assessment, StealthCoder runs invisibly as a safety net while you work.
The problem
Histogram bars have width one. A square of side k fits when some k consecutive bars each have height at least k. Return the maximum square area k*k. Function largestSquareArea(heights: int[]) → int Examples Example 1 heights = [1,2,3,3] return = 4 Two adjacent bars have height at least two. Example 2 heights = [3,3,3] return = 9 All three bars support a side-three square. Constraints 1 <= heights.length <= 100000 0 <= heights[i] <= 100000
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: find the largest k such that some k consecutive bars all have height at least k. That's equivalent to the max over bars of min(height, width of the span where that bar is the minimum). Two clean routes. First, sort heights descending and walk it: at index i (0-based), you have i+1 bars with height at least sorted[i], so candidate side is min(sorted[i], i+1). But that ignores adjacency, so it's wrong here. Pitfall found. Use a monotonic stack to get, for each bar, the span where it's the minimum, then side = min(height, span). Or binary search on k and check for a run of k bars with height >= k in O(n). Both are O(n) or O(n log n), fine for 100000 bars. Return side squared, and watch for zeros. If the live OA freezes you, StealthCoder is the hedge.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Largest Square in a Histogram 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Largest Square in a Histogram FAQ
What's the trick in the ZipRecruiter largest square histogram problem?+
Bars must be consecutive, so sorting alone fails. For each bar, find the span where it's the minimum height using a monotonic stack. The best square side from that bar is min(height, span width). Take the max side over all bars and square it.
Is binary search a valid approach here?+
Yes. If a square of side k fits, a square of side k-1 fits too, so feasibility is monotonic. Binary search k from 0 to n, and for each k scan once to check for a run of k consecutive bars with height at least k. Total O(n log n).
What's the most common mistake?+
Returning the largest rectangle area or ignoring adjacency. Another slip is returning k instead of k*k. Check Example 1: [1,2,3,3] gives side 2, so the answer is 4, not 2 or 6.
How do I prepare in 48 hours for this?+
Practice the monotonic stack on the classic largest rectangle in histogram, then adapt it by taking min(height, width). Also write the binary search version once. Test on [1,2,3,3], [3,3,3], and an all-zeros array before the assessment.
What edge cases should I test?+
Test a single bar, all zeros, a height larger than the array length like [100000], and strictly increasing bars. Heights can reach 100000 and k*k can hit 10 billion in theory, though width caps k at the array length. Still use a 64-bit-safe type if your language needs it.