Skip to content
AI360Xpert
Beta
Difficulty: MediumMath & Geometry

Detect Squares

Problem in Plain English

You must support adding points from a stream, where duplicates count as distinct points, and answering how many axis-aligned squares use a given query point as one corner. Return the count for each query, weighing every choice of stored duplicates separately.

Problem Statement

Implement a point-stream data structure with an initially empty DetectSquares instance. add(point) stores one occurrence of [x,y]; repeated coordinates are distinct selectable occurrences. count(point) returns how many unordered triples of stored occurrences form a positive-area axis-aligned square together with that supplied query corner.

All four corners must be at distinct coordinates, both horizontal and vertical side lengths must match, and edges must be parallel to the coordinate axes. The query need not have been added. Stored copies of the query coordinate do not multiply the answer: the supplied query is one fixed corner. Adding stores an owned coordinate copy or immutable key, so callers can reuse their point array; counting preserves the query and the stored state.

Constraints

  • Each add or count point contains exactly two integers [x,y].
  • Both coordinates are between 0 and 1000 inclusive.
  • At most 3000 calls to add and count combined are made on an instance.

Examples

Example 1

Input
["DetectSquares","add","add","add","count","count","add","count"] [[],[[3,10]],[[11,2]],[[3,2]],[[11,10]],[[14,8]],[[11,2]],[[11,10]]]
Output
[null,null,null,null,1,0,null,2]

After adding [3,10], [11,2] and [3,2], count([11,10]) finds one square with side 8. count([14,8]) finds none. Adding a duplicate [11,2] raises count([11,10]) to 2.

Example 2

Input
["DetectSquares","count","add","add","add","count"] [[],[[0,0]],[[1,0]],[[0,1]],[[1,1]],[[0,0]]]
Output
[null,0,null,null,null,1]

Counting in an empty structure returns zero. Three later additions form a unit square with query [0,0], even though that query coordinate was never stored.

Example 3

Input
["DetectSquares","add","add","add","add","add","add","add","count"] [[],[[1,0]],[[1,0]],[[0,1]],[[0,1]],[[0,1]],[[1,1]],[[0,0]],[[0,0]]]
Output
[null,null,null,null,null,null,null,null,6]

With fixed query [0,0], there are 2 choices for [1,0], 3 for [0,1], and 1 for [1,1], making 6 triples. The stored [0,0] is not one of the other three corners and does not multiply the answer.

Example 4

Input
["DetectSquares","add","add","add","count"] [[],[[2,0]],[[0,1]],[[2,1]],[[0,0]]]
Output
[null,null,null,null,0]

These corners form a rectangle of width 2 and height 1, so they do not qualify as a square despite all four coordinates being distinct.

Example 5

Input
["DetectSquares","add","add","add","count","count"] [[],[[0,1000]],[[1000,0]],[[1000,1000]],[[0,0]],[[0,0]]]
Output
[null,null,null,null,1,1]

The coordinate bounds allow a square of side 1000. Repeated counts return the same value because count does not add its query or consume stored occurrences.

Intuition

Once the query corner and its opposite corner are fixed, an axis-aligned square has no freedom left: the other corners combine their x and y coordinates. In Example 1 the query [11,10] and diagonal [3,2] force [3,10] and [11,2], with side length 8. Adding another [11,2] creates another choice for that one stored corner, doubling the count without changing the square geometry. The query is supplied once and is not drawn from stored duplicates.

Approaches

Triple Loop Check

Solution Details

Reveal Triple Loop Check: intuition, complexity, and code

Hints

Hint 1
A square is fixed once you know two opposite corners.
Hint 2
For a fixed query point, each stored point is a candidate for the opposite corner, and the other two corners are then forced.
Hint 3
Counting duplicates with a frequency map and multiplying the corner counts handles repeated points without extra loops.

Edge Cases

  • An empty structure or fewer than three stored occurrences always returns 0.
  • The query does not need to be present; stored duplicates at the query coordinate do not multiply answers.
  • Duplicate diagonal and adjacent corners multiply independently: counts 2,3,4 give 24 triples.
  • Same-row/column candidates and repeated coordinates cannot form positive-area squares.
  • Rectangles with unequal side lengths and rotated squares are rejected: only equal-sided, axis-aligned grids count.
  • Diagonals can lie in all four quadrants relative to the query; compare absolute differences.
  • Coordinates 0 and 1000 can be opposite edges of a side-1000 square.
  • Mutating a point array after add must not change stored coordinates; repeated count calls must not modify the structure.

Common Mistakes and Interview Tips

  • Replacing frequencies with a set loses distinct duplicate choices.
  • Scanning duplicate occurrences and also multiplying by the diagonal frequency double-counts that corner. Scan distinct keys once.
  • Multiplying by the stored query frequency is wrong: the supplied query is one fixed corner and may not be stored at all.
  • Counting all permutations of a stored triple overcounts by six; the reference approach uses increasing indices.
  • Using equal signed differences misses diagonals in two quadrants. Use absolute differences and reject a zero difference.
  • Accepting merely two x values and two y values without four distinct coordinates can admit missing or repeated corners.
  • Retaining the caller’s mutable point array makes later edits corrupt the stored stream. Copy its values or build an immutable key.
  • A count lookup should use a noninserting read; adding missing keys while iterating mutates state and may invalidate iteration.
  • Interview tip: give method-specific complexities and distinguish n additions from u unique coordinates; duplicates change counts without necessarily increasing scan cost.

Key Takeaway

For a fixed geometric query, find a unique structural witness such as the opposite corner. It forces remaining coordinates, allowing frequency lookups and a product of independent duplicate choices instead of enumerating every tuple.