Magicsheet logo

Minimum Rectangles to Cover Points

Medium
37.5%
Updated 8/1/2025

Asked by 1 Company

Minimum Rectangles to Cover Points

What is this problem about?

The Minimum Rectangles to Cover Points problem provides a list of 2D points and a fixed rectangle width W. You need to place axis-aligned rectangles of width W (and any height) to cover all points, minimizing the number of rectangles used. Rectangles can be placed anywhere, but each must cover points within a horizontal span of W. This Minimum Rectangles to Cover Points interview question is a greedy interval-covering problem.

Why is this asked in interviews?

Microsoft asks this to evaluate greedy strategy design for geometric coverage problems. The ability to simplify a 2D placement problem by focusing on one dimension (x-coordinates) while the other is unconstrained (y-coordinate) shows strong problem decomposition. The array, sorting, and greedy interview pattern applies elegantly here.

Algorithmic pattern used

The key insight is that y-coordinates are irrelevant — since rectangles have unlimited height, a single rectangle can cover all points sharing a similar x-range. Sort all points by x-coordinate. Greedily place a rectangle starting at the leftmost uncovered point, extending it W units to the right, covering all points within [x, x + W]. Then move to the next uncovered point and repeat. Count total rectangles placed.

Example explanation

Points: [(1,2), (3,5), (6,1), (8,3), (10,4)], W = 4.

  • Start at x=1, cover [1, 5]: covers (1,2) and (3,5). Rectangle 1.
  • Next uncovered: x=6, cover [6, 10]: covers (6,1), (8,3), (10,4). Rectangle 2.
  • Total: 2 rectangles.

Common mistakes candidates make

  • Considering y-coordinates unnecessarily, which complicates the solution.
  • Not sorting points before applying the greedy sweep.
  • Placing the rectangle center at the first point instead of aligning its left edge.
  • Miscounting points covered by the last rectangle's boundary.

Interview preparation tip

When a 2D geometry problem has unconstrained movement in one axis, always look to reduce it to a 1D problem. Sorting by the constrained dimension and applying a greedy sweep is a powerful pattern that appears in interval scheduling, painting, and coverage problems. Practice by solving interval covering and merging problems — they build the same intuition needed for Minimum Rectangles to Cover Points.

Similar Questions