Projection Area of 3D Shapes

EasyMatrixmath

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

An n x n grid holds towers of unit cubes aligned with the axes: grid[i][j] is the height of the tower standing on cell (i, j). Project the shape onto the three coordinate planes: looking from the top (xy), from the front (yz), and from the side (zx). Each projection is the 2D shadow of the cubes. Return the total area of the three projections.

Examples

Input: grid = [[1,2],[3,4]]
Output: 17
Top view: 4 non-empty cells. Row maxima 2 + 4 = 6. Column maxima 3 + 4 = 7. Total 17.
Input: grid = [[2]]
Output: 5
Top view 1, and each side view is a column of height 2.
Input: grid = [[1,0],[0,2]]
Output: 8
Top 2, row maxima 1 + 2 = 3, column maxima 1 + 2 = 3.

Constraints

  • n == grid.length == grid[i].length
  • 1 <= n <= 50
  • 0 <= grid[i][j] <= 50

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More math problems