As reported: American Express on-campus online assessment (2026): the second of three implementation-heavy coding questions — geometry with a sliding window / two pointers.
You are given n points points[i] = [x, y] with integer coordinates and an integer P. Place one axis-aligned rectangle whose perimeter is at most P. Return the largest number of points the rectangle can contain.
Clarifications
- A rectangle with width
wand heighthhas perimeter2·(w + h);wandhmay be0(a segment or a single point is allowed). - Points on the border count as inside.
- Points may repeat; each copy counts.
- "Perimeter exactly
P" gives the same answer: a smaller rectangle can always be widened without losing points.
Example 1
points = [[1,1],[2,3],[4,2],[6,6],[3,1]], P = 10 -> 4
The rectangle from (1,1) to (4,3) has width 3 and height 2, perimeter 2·(3+2) = 10, and contains every point except (6,6).
Example 2
points = [[0,0],[5,5],[10,0]], P = 4 -> 1
Any two points are at least 10 apart in w + h, which needs perimeter 20.
Constraints: 1 ≤ n ≤ 150; 0 ≤ x, y ≤ 10^4; 0 ≤ P ≤ 4 · 10^4.
Code runner format: two lines: points as a JSON array of [x, y] pairs, then P. Implement maxPointsInRectangle(points, perimeter); the harness prints the returned number.