Count Rectangle Coverage on a Grid

Last updated: July 21, 2026

Quick Overview

The candidate is asked to write a function that counts the number of rectangles that can be formed on a given grid based on specified coordinates.

Google
Coding & Algorithms
Software Engineer
Google
July 21, 2026
Software Engineer
Onsite
Coding & Algorithms
Easy

60

0

481 solved


The candidate is asked to write a function that counts the number of rectangles that can be formed on a given grid based on specified coordinates.

This question is typically asked during the onsite coding round to evaluate a candidate's problem-solving skills and ability to work with algorithms. The interviewer is looking for a clear understanding of grid-based problems and how to efficiently count combinations of points to form rectangles. The candidate's approach to coding, optimization, and edge cases will also be assessed.

What the Interviewer Expects
  • Clearly explain the approach before coding.
  • Identify the coordinates that define the rectangles.
  • Implement a solution that efficiently counts rectangles.
  • Discuss time and space complexity of the solution.
  • Handle edge cases and provide test cases.
Key Topics to Cover
Grid-based algorithms
Combinatorial counting
Time complexity analysis
Space complexity analysis
Edge case handling
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
  • How would you optimize your solution for larger grids?
  • Can you explain how you would handle overlapping rectangles?
  • What would you change if the grid size was dynamic?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Approach

To count the number of rectangles that can be formed on a grid, we need to identify pairs of coordinates. Each rectangle can be defined by two opposite corners (top-left and bottom-right). Therefore, ...

Algorithm
  1. Create a set to store unique points from the grid.
  2. Iterate through all pairs of points in the set.
  3. For each pair, check if the other two corners of the rectangle exist in the set.
  4. If th...

Submit Your Answer
Markdown supported

Related Questions