2013. Detect Squares

Detect Squares (LeetCode 2013) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.

How to solve Detect Squares

Detect Squares is a Data Structure Design problem, part of Design Patterns. Design and implement data structures: LRU cache, LFU cache, HashMap, and specialized structures.

Work towards the solution in these steps:

  1. Maintain the frequency of all the points in a hash map.
  2. Traverse the hash map and if any point has the same y-coordinate as the query point, consider this point and the query point to form one of the horizontal lines of the square.

The reference solution runs in O(N) time and O(U) space.

The Detect Squares solution and editorial walks through the full approach with reference code.

Problem statement

You are given a stream of points on the X-Y plane. Design an algorithm that:

- Adds new points from the stream into a data structure. Duplicate points are allowed and should be treated as different points.

- Given a query point, counts the number of ways to choose three points from the data structure such that the three points and the query point form an axis-aligned square with positive area.

An axis-aligned square is a square whose edges are all the same length and are either parallel or perpendicular to the x-axis and y-axis.

Implement the DetectSquares class:

- DetectSquares() Initializes the object with an empty data structure.

- void add(int[] point) Adds a new point point = [x, y] to the data structure.

- int count(int[] point) Counts the number of ways to form axis-aligned squares with point point = [x, y] as described above.

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]

Explanation DetectSquares detectSquares = new DetectSquares(); detectSquares.add([3, 10]); detectSquares.add([11, 2]); detectSquares.add([3, 2]); detectSquares.count([11, 10]); // return 1. You can choose: // - The first, second, and third points detectSquares.count([14, 8]); // return 0. The query point cannot form a square with any points in the data structure. detectSquares.add([11, 2]); // Adding duplicate points is allowed. detectSquares.count([11, 10]); // return 2. You can choose: // - The first, second, and third points // - The first, third, and fourth points

Constraints:

- point.length == 2

- 0 <= x, y <= 1000

- At most 3000 calls in total will be made to add and count.

Topics and companies

Topics: Array, Hash Table, Design, Counting.

How to practise Detect Squares on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(N) time and O(U) space.

Open Detect Squares in the code editor, or read the Detect Squares editorial for a worked solution with its approach and complexity analysis.

Where Detect Squares sits in the DSA pattern sheet

Problems related to Detect Squares

Other problems that use the same Design (General/Specific) and Design Patterns techniques:

Detect Squares FAQ

What is the LeetCode number of Detect Squares?

Detect Squares is LeetCode problem 2013, rated Medium.

What pattern is used to solve Detect Squares?

Detect Squares is a Data Structure Design problem. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.

What is the time complexity of Detect Squares?

The reference solution runs in O(N) time and O(U) space.

Preparing this workspace.