Cut a Brick Wall Vertically Through the Fewest Bricks

Last updated: November 29, 2026

Quick Overview

A wall is given as rows of brick widths, every row having the same total width. Draw one vertical line from top to bottom, not along the outer edges, that crosses the fewest bricks, and return that count.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

0

0 solved


A wall is given as rows of brick widths, every row having the same total width. Draw one vertical line from top to bottom, not along the outer edges, that crosses the fewest bricks, and return that count.

A Google phone screen favorite where the trick is reframing: instead of counting crossed bricks, count aligned edges. The interviewer checks that you hash prefix-sum positions and avoid iterating over the full width, which may be huge.

What the Interviewer Expects
  • Reframes crossings as rows minus aligned edges
  • Computes per-row prefix sums of widths
  • Counts edge positions with a hash map, excluding the right edge
  • Avoids iterating over every possible x coordinate
  • Handles rows of a single brick
Key Topics to Cover
Hash Map
Prefix Sums
Reframing
Counting
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
  • What if the wall width is up to 10^9 but there are few bricks?
  • Find the best k vertical lines that together cross the fewest bricks.
  • What if rows can have different total widths?
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: wall, a list of rows, each a list of positive widths; every row sums to the same width W.
  • Output: the minimum number of bricks a vertical line at an integer position 0 < x < W must cross....
High-Level Design

A line at position x crosses a brick in a row unless x is one of that row's internal edge positions. So crossings at x = rows - (number of rows with an edge at x). Minimizing crossings means maximizin...


Submit Your Answer
Markdown supported