PracHub
QuestionsLearningGuidesInterview Prep
|Home/Coding & Algorithms/Google

Count Rectangle Coverage on a Grid

Last updated: Jul 28, 2026

Quick Overview

Apply many inclusive rectangular increments to a square grid and return the final coverage count for every cell. The exercise emphasizes upper-bound efficiency, duplicate updates, empty input, edge coordinates, non-mutating behavior, and a clear complexity analysis.

  • easy
  • Google
  • Coding & Algorithms
  • Software Engineer

Count Rectangle Coverage on a Grid

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Onsite

## Count Rectangle Coverage on a Grid You are given an `n x n` grid initially filled with zeroes and a list of inclusive rectangular updates. Each rectangle adds one to every covered cell. Implement: ```text range_add_counts(n, rectangles) -> list[list[int]] ``` Each rectangle is `[row1, col1, row2, col2]`, using zero-based inclusive coordinates. Return the final grid, where each cell contains the number of rectangles covering it. ### Example ```text n = 3 rectangles = [ [0, 0, 1, 1], [1, 1, 2, 2] ] result = [ [1, 1, 0], [1, 2, 1], [0, 1, 1] ] ``` Cell `(1, 1)` is covered by both rectangles. ### Constraints - `1 <= n <= 500` - `0 <= len(rectangles) <= 100000` - `0 <= row1 <= row2 < n` - `0 <= col1 <= col2 < n` - Duplicate rectangles count as separate updates. - If `rectangles` is empty, return an all-zero grid. ### Clarifications - Both lower and upper rectangle coordinates are included. - The input list must not be mutated. - Returning a newly allocated matrix is expected. ### Hints - Updating every cell of every rectangle is too slow at the upper bound. - A two-dimensional difference array can encode a rectangle with four boundary changes. - Recover cell values by taking prefix sums across both dimensions, taking care at the grid edges. ### Discussion Extensions - Derive the time and space complexity. - How would you adapt the method if coordinates were very large but only a small number of points were queried? - How would the boundary updates change for half-open rectangles?

Quick Answer: Apply many inclusive rectangular increments to a square grid and return the final coverage count for every cell. The exercise emphasizes upper-bound efficiency, duplicate updates, empty input, edge coordinates, non-mutating behavior, and a clear complexity analysis.

Related Interview Questions

  • Deduplicate and Order Batch and Streaming Logs - Google (medium)
  • Count Overlapping Rectangle Updates on a Grid - Google (hard)
  • Find A Threshold-Limited Path With Minimum Required Safety - Google (medium)
  • Filter Repeated Robot Status Messages - Google (medium)
|Home/Coding & Algorithms/Google

Count Rectangle Coverage on a Grid

Google logo
Google
Jul 21, 2026, 12:00 AM
easySoftware EngineerOnsiteCoding & Algorithms
0
0

Count Rectangle Coverage on a Grid

You are given an n x n grid initially filled with zeroes and a list of inclusive rectangular updates. Each rectangle adds one to every covered cell.

Implement:

range_add_counts(n, rectangles) -> list[list[int]]

Each rectangle is [row1, col1, row2, col2], using zero-based inclusive coordinates. Return the final grid, where each cell contains the number of rectangles covering it.

Example

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

result = [
  [1, 1, 0],
  [1, 2, 1],
  [0, 1, 1]
]

Cell (1, 1) is covered by both rectangles.

Constraints

  • 1 <= n <= 500
  • 0 <= len(rectangles) <= 100000
  • 0 <= row1 <= row2 < n
  • 0 <= col1 <= col2 < n
  • Duplicate rectangles count as separate updates.
  • If rectangles is empty, return an all-zero grid.

Clarifications

  • Both lower and upper rectangle coordinates are included.
  • The input list must not be mutated.
  • Returning a newly allocated matrix is expected.

Hints

  • Updating every cell of every rectangle is too slow at the upper bound.
  • A two-dimensional difference array can encode a rectangle with four boundary changes.
  • Recover cell values by taking prefix sums across both dimensions, taking care at the grid edges.

Discussion Extensions

  • Derive the time and space complexity.
  • How would you adapt the method if coordinates were very large but only a small number of points were queried?
  • How would the boundary updates change for half-open rectangles?

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Google•More Software Engineer•Google Software Engineer•Google Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.