Magicsheet logo

Surface Area of 3D Shapes

Easy
12.5%
Updated 8/1/2025

Asked by 2 Companies

Surface Area of 3D Shapes

What is this problem about?

The Surface Area of 3D Shapes coding problem asks you to calculate the total surface area of a 3D figure represented on a 2D grid. Each cell (i, j) in the grid contains a value representing the number of cubes stacked at that position. Each cube has a side length of 1. You need to calculate the area of all exposed faces of these cubes.

Why is this asked in interviews?

This "Easy" level problem is common in Amazon and Google interviews to test basic geometry and matrix manipulation. It requires a systematic approach to counting—considering the top and bottom faces, and then the side faces while accounting for the overlap between adjacent stacks. It evaluates your ability to handle grid-based indexing and your attention to detail in a 3D context.

Algorithmic pattern used

The algorithmic pattern used is Matrix Iteration and Geometry. The calculation can be broken down:

  1. For each stack (if height > 0), add 2 for the top and bottom surfaces.
  2. For each stack, add the surface area of the four sides (4 * height).
  3. Subtract the hidden surface area caused by adjacent stacks. For each stack, subtract min(current_height, neighbor_height) for each of the four neighbors. Alternatively, just look at each stack and its four neighbors: the contribution of one side of a stack is max(0, current_height - neighbor_height).

Example explanation

Grid: [[2]]

  • Stack at (0,0) has height 2.
  • Top and bottom: 2.
  • Sides (no neighbors): 4 * 2 = 8.
  • Total = 10. Grid: [[1, 2]]
  • Stack 1 (height 1): top/bottom = 2, sides = 4*1 = 4. Subtotal 6.
  • Stack 2 (height 2): top/bottom = 2, sides = 4*2 = 8. Subtotal 10.
  • Overlap: min(1, 2) * 2 faces = 2.
  • Total = 6 + 10 - 2 = 14.

Common mistakes candidates make

A common mistake is forgetting the top and bottom faces for each stack. Another error is double-counting or missing the overlapping areas between adjacent stacks. Candidates sometimes forget that if a stack is at the boundary of the grid, its outer side is fully exposed.

Interview preparation tip

When preparing for the Surface Area of 3D Shapes interview question, practice iterating through 2D grids and accessing neighbors (up, down, left, right). Use an array of offsets like [(0, 1), (0, -1), (1, 0), (-1, 0)] to make your neighbor-checking code cleaner and less error-prone. This problem is a great way to practice the Matrix interview pattern.

Similar Questions