Skip to content

Instantly share code, notes, and snippets.

@mgild
Last active November 7, 2025 16:42
Show Gist options
  • Save mgild/a1d2a2386fa801801e306b6947834dae to your computer and use it in GitHub Desktop.
Save mgild/a1d2a2386fa801801e306b6947834dae to your computer and use it in GitHub Desktop.

Technical Interview Question: Counting Squares from Points

Prompt

You are given a vector of points in 2D space, represented as Vec<(i32, i32)>. Write a function that returns the number of distinct squares that can be formed using these points as vertices.

A square is defined as a quadrilateral with four equal sides and four right angles.


Function Signature (Rust)

fn count_squares(points: &Vec<(i32, i32)>) -> usize

Examples

Example 1

let points = vec![(0, 0), (1, 0), (0, 1), (1, 1)];
assert_eq!(count_squares(&points), 1);

Explanation: These four points form a unit square.

Example 2

let points = vec![
    (0, 0), (1, 0), (2, 0),
    (0, 1), (1, 1), (2, 1),
    (0, 2), (1, 2), (2, 2)
];
assert_eq!(count_squares(&points), 6);

Explanation:

  • Four 1×1 squares
  • One 2×2 square
  • One √2×√2 rotated square (diamond)

Example 3

let points = vec![(0, 0), (1, 1), (2, 0)];
assert_eq!(count_squares(&points), 0);

Explanation: No four points form a square.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment