Problem · Matrix

Inclusive Rectangle Coverage Counts

MediumGoogleFULLTIMEONSITE INTERVIEW
See Google hiring insights

Problem statement

You are given an integer n and an array rectangles. Start with an n x n matrix of zeros.

Each rectangle is represented as [top, left, bottom, right] using zero-based row and column indices. Its boundaries are inclusive. For every rectangle, add 1 to every matrix cell whose row is between top and bottom and whose column is between left and right.

The problem statement continues
Pro

Examples

Example 1

n = 3rectangles = [[0,0,1,1],[1,1,2,2]]return = [[1,1,0],[1,2,1],[0,1,1]]

The two rectangles overlap only at (1,1), so that cell has count 2. Cells covered by one rectangle have count 1.

FastPrep Pro
Reported in 1 Google interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 2 more worked examples, explained
  • Guided hints and editorial
  • Run your code on real test cases
$9/month

Pro subscription, billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week
CodePython 3
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 Google interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 2 more worked examples, explained
  • Guided hints and editorial
  • Run your code on real test cases
$9/month

Pro subscription, billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week