Medium · System Design

Detect Squares

Given a sequence of add(point) and count(point) calls on 2D integer points, where repeated additions count as separate points, return for each count call the number of ways to choose three added points that form an axis-aligned square of positive area together with the query point.

Examples

Example 1

{
  "ops": [
    {"kind": "add", "point": [0, 0]},
    {"kind": "add", "point": [0, 1]},
    {"kind": "add", "point": [1, 0]},
    {"kind": "count", "point": [1, 1]}
  ]
}

Output: 1

Example 2

{
  "ops": [
    {"kind": "add", "point": [3, 10]},
    {"kind": "add", "point": [11, 2]},
    {"kind": "add", "point": [3, 2]},
    {"kind": "count", "point": [11, 10]}
  ]
}

Output: 1

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More System Design problems