Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Rectangles From Grid Points
00:00
5 left

Rectangles From Grid Points

MediumPython

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):
Interviewer

Your question is Rectangles From Grid Points. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.