Cover the Most Points with an Axis-Aligned Rectangle of Bounded Perimeter
Company: American
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Overview: Maximize point coverage with an axis-aligned rectangle under a perimeter budget, including boundary points and repeated coordinates.
Read the full American Software Engineer interview experience this question came from
Constraints
- 0 <= len(points) <= 200
- Each point is [x, y] with integer coordinates in [-1000000, 1000000]
- 0 <= P <= 1000000000
- Repeated coordinates count as separate input points
- The rectangle is axis-aligned; its width and height are nonnegative real numbers satisfying 2 * (width + height) <= P, and zero width or zero height is allowed
- Rectangle coordinates need not be integers, and points on the boundary count as covered
- Return 0 when there are no points
Examples
Input: ([], 0)
Expected Output: 0
Explanation: No points at all, so the answer is 0.
Input: ([[5, -3]], 0)
Expected Output: 1
Explanation: A single point is covered by a degenerate 0-by-0 rectangle of perimeter 0 <= 0.
Hints
- A rectangle that covers a set of points can be shrunk until each side touches one of those points, so only the spans between input coordinates ever matter.
- The perimeter budget ties width and height together: once you commit to a horizontal extent, the rest of the budget fixes the largest vertical extent you may still use.
- All coordinates are integers, so the entire feasibility test 2 * (width + height) <= P can be done in integer arithmetic; check the P = 0 case and repeated points before you are done.