FastPrepCount Axis-Aligned and Rotated Squares

Count Axis-Aligned and Rotated Squares

Google logoGoogle● HardFULLTIMEPHONE SCREEN
Learn

Problem statement

Each row in points is an integer coordinate. Count all squares whose four vertices occur in the input, including rotated squares.

Duplicate coordinates represent distinct point occurrences. A square using coordinates with frequencies f1..f4 contributes their product. Return the total number of occurrence-level squares.

Function

countSquares(points: int[][]) → long

Examples

Example 1

points = [[0,0],[1,0],[1,1],[0,1]]return = 1

The four points form one axis-aligned square.

Example 2

points = [[0,1],[1,0],[2,1],[1,2]]return = 1

The four points form one rotated square.

Constraints

  • 0 <= points.length <= 2000.
  • Coordinates fit signed 32-bit integers.
  • The answer fits signed 64-bit range.

Source note: Author-written prompt excerpt retained for source fidelity.

More Google problems

See Google hiring insights
public long countSquares(int[][] points) {
    // Write your code here.
}
points[[0,0],[1,0],[1,1],[0,1]]
expected1
Checking account…