Rectangles Area - Problem

Imagine you have a collection of points scattered across a coordinate plane, and you want to find all possible axis-aligned rectangles that can be formed using any two of these points as opposite corners!

Given a table Points with columns:

  • id - unique identifier for each point
  • x_value - x-coordinate of the point
  • y_value - y-coordinate of the point

Your mission: Find all possible rectangles with non-zero area that can be formed by selecting any two points as opposite corners. Each rectangle must be axis-aligned (sides parallel to x and y axes).

Return format: Each row should contain (p1, p2, area) where:

  • p1 and p2 are the IDs of the two corner points
  • area is the calculated area (must be > 0)

Sorting: Order by area (descending), then by p1 (ascending), then by p2 (ascending).

Input & Output

example_1.sql โ€” Basic Rectangle Formation
$ Input: Points table: +----+---------+---------+ | id | x_value | y_value | +----+---------+---------+ | 1 | 1 | 1 | | 2 | 1 | 3 | | 3 | 3 | 1 | | 4 | 3 | 3 | +----+---------+---------+
โ€บ Output: +----+----+------+ | p1 | p2 | area | +----+----+------+ | 1 | 4 | 4 | | 2 | 3 | 4 | +----+----+------+
๐Ÿ’ก Note: Points (1,1) and (3,3) form a 2ร—2 rectangle with area 4. Points (1,3) and (3,1) also form a 2ร—2 rectangle with area 4. Both have same area, so ordered by p1.
example_2.sql โ€” Different Sized Rectangles
$ Input: Points table: +----+---------+---------+ | id | x_value | y_value | +----+---------+---------+ | 1 | 0 | 0 | | 2 | 4 | 0 | | 3 | 0 | 3 | | 4 | 4 | 4 | +----+---------+---------+
โ€บ Output: +----+----+------+ | p1 | p2 | area | +----+----+------+ | 1 | 4 | 16 | | 2 | 3 | 12 | +----+----+------+
๐Ÿ’ก Note: Point (0,0) and (4,4) create a 4ร—4 rectangle (area=16). Point (4,0) and (0,3) create a 4ร—3 rectangle (area=12). Ordered by area descending.
example_3.sql โ€” No Valid Rectangles
$ Input: Points table: +----+---------+---------+ | id | x_value | y_value | +----+---------+---------+ | 1 | 1 | 1 | | 2 | 1 | 2 | | 3 | 1 | 3 | +----+---------+---------+
โ€บ Output: +----+----+------+ | p1 | p2 | area | +----+----+------+ (empty result)
๐Ÿ’ก Note: All points have the same x-value (1), so no valid axis-aligned rectangles can be formed. Need points with different x AND y coordinates.

Constraints

  • 2 โ‰ค number of points โ‰ค 500
  • 0 โ‰ค x_value, y_value โ‰ค 106
  • All point coordinates are integers
  • id values are unique for each point
  • Points may have duplicate coordinates (same x_value and y_value)

Visualization

Tap to expand
xyP1(2,4)P2(6,2)P3(9,5)P4(4,3)Area = 4ร—2 = 8Area = 7ร—1 = 7Resultsp1=1, p2=2, area=8p1=1, p2=3, area=7p1=2, p4=4, area=4p1=3, p2=4, area=10Sorted by area DESC
Understanding the Visualization
1
Identify Valid Pairs
Two points can form a rectangle only if they have different x-coordinates AND different y-coordinates
2
Visualize Rectangle
The two points become opposite corners of an axis-aligned rectangle
3
Calculate Area
Area equals the absolute difference in x-values times the absolute difference in y-values
4
Sort and Return
Order results by area (descending), then by first point ID, then by second point ID
Key Takeaway
๐ŸŽฏ Key Insight: Use SQL self-join to examine all point pairs, filter for different x AND y coordinates, calculate rectangular areas, and sort results appropriately.
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
23.9K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen