Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rectangles From Grid Points

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Rectangles From Grid Points. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

MITRE ATT&CK visualization tools may represent findings as points on a coordinate plane. Given a collection of distinct two-dimensional points, return the number of distinct rectangles whose four sides are parallel to the x-axis or y-axis. A rectangle is counted only when all four of its corner points are present.

Formal Specification

Implement count_rectangles(points), where points is a list of coordinate pairs [x, y]. Return an integer. Points may be provided in any order, and no point is duplicated. Rectangles must have positive width and height. Multiple rectangles may share corners or sides.

Your algorithm should handle large point sets efficiently. Avoid checking every quadruple of points directly. One effective strategy is to group points by x-coordinate, generate every pair of y-coordinates within each vertical column, and count how many columns contain each pair.

Constraints

  • 1 <= len(points) <= 2 * 10^4
  • Each point is a two-element list [x, y]
  • -10^9 <= x, y <= 10^9
  • All points are distinct
  • Coordinates are integers

Function Signature

def count_rectangles(points):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output