Count Axis-Aligned Squares in a Point Stream

Quick Overview

Count Axis-Aligned Squares in a Point Stream. Implement persistent DetectSquares. add(point) inserts a point occurrence; duplicate coordinates remain distinct choices. count(point) returns the number of ways to select three stored point occurrences which, with the query point, form a positive-area square whose sides are parallel to the coordinate axes. The query point is supplied separately and need not already be stored.

Count Axis-Aligned Squares in a Point Stream

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Implement the persistent `DetectSquares` class. `add(point)` stores a new point occurrence, with duplicate coordinates treated as separate choices. `count(point)` returns how many selections of three stored occurrences form a positive-area axis-aligned square together with the supplied query point. The query point is an additional corner and need not have been added; counting does not insert it. ```cpp class DetectSquares { public: DetectSquares(); void add(vector<int> point); int count(vector<int> point); }; ``` ### Examples **Example 1:** ```text Calls: add([0,0]), add([0,2]), add([2,0]), count([2,2]) Results: void, void, void, 1 Explanation: The three stored corners complete a side-two square. ``` **Example 2:** ```text Calls on a fresh object: add([1,1]), add([1,3]), add([3,1]), add([3,1]), count([3,3]), count([1,1]) Results: void, void, void, void, 2, 0 Explanation: Either occurrence at [3,1] completes the first query; [3,3] is absent for the second. ``` ### Constraints - Every point has two coordinates in `[0,1000]`. - At most 3000 calls to add and count in total. ### Clarifying Questions Rotated squares and zero-area shapes do not count. Counting does not insert the query point. ```hint Inspect the structure If you choose the diagonal corner opposite the query, are the other two corners forced? ``` ### Reasoning checkpoints - Count only positive-area squares with sides parallel to the coordinate axes. - Preserve stored-point multiplicity and count each valid selection of three occurrences once. - Maintain the original persistent constructor and add/count protocol. ### Optional extensions How could you reduce query work when many distinct points have been stored?

Overview: Count Axis-Aligned Squares in a Point Stream. Implement persistent DetectSquares. add(point) inserts a point occurrence; duplicate coordinates remain distinct choices. count(point) returns the number of ways to select three stored point occurrences which, with the query point, form a positive-area square whose sides are parallel to the coordinate axes. The query point is supplied separately and need not already be stored.

Amazon logo
Amazon
May 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Implement the persistent DetectSquares class. add(point) stores a new point occurrence, with duplicate coordinates treated as separate choices. count(point) returns how many selections of three stored occurrences form a positive-area axis-aligned square together with the supplied query point. The query point is an additional corner and need not have been added; counting does not insert it.

class DetectSquares {
public:
    DetectSquares();
    void add(vector<int> point);
    int count(vector<int> point);
};

Examples

Example 1:

Calls: add([0,0]), add([0,2]), add([2,0]), count([2,2])
Results: void, void, void, 1
Explanation: The three stored corners complete a side-two square.

Example 2:

Calls on a fresh object: add([1,1]), add([1,3]), add([3,1]), add([3,1]), count([3,3]), count([1,1])
Results: void, void, void, void, 2, 0
Explanation: Either occurrence at [3,1] completes the first query; [3,3] is absent for the second.

Constraints

  • Every point has two coordinates in [0,1000] .
  • At most 3000 calls to add and count in total.

Clarifying Questions Guidance

Rotated squares and zero-area shapes do not count. Counting does not insert the query point.

Reasoning checkpoints

  • Count only positive-area squares with sides parallel to the coordinate axes.
  • Preserve stored-point multiplicity and count each valid selection of three occurrences once.
  • Maintain the original persistent constructor and add/count protocol.

Optional extensions

How could you reduce query work when many distinct points have been stored?