Quick Overview

A geometry counting problem: given integer centers of axis-aligned squares with side length 2, count the unordered pairs that overlap, where touching along an edge or at a single corner counts. It tests turning the geometry into a coordinate condition and counting pairs faster than a quadratic scan for up to 100,000 squares.

Count Overlapping Pairs of Side-2 Squares Given Their Centers

Company: Capital One

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Online Assessment

You are given the centers of `n` axis-aligned squares on a plane, each with side length 2. The square centered at `(x, y)` covers every point `(u, v)` with `x - 1 <= u <= x + 1` and `y - 1 <= v <= y + 1`. Count the pairs of squares that overlap. Squares that only touch along an edge or at a single corner count as overlapping. ### Function Signature ```python def count_overlapping_square_pairs(centers: list[list[int]]) -> int: ``` ### Rules - Pairs are unordered and counted once: count index pairs `(i, j)` with `i < j`. - Two squares overlap when they share at least one point, boundary points included. - Centers may repeat, and squares with the same center overlap. - All squares are axis-aligned; none is rotated. ### Constraints - `1 <= n <= 10^5`, where `n = len(centers)` - `centers[i] = [x, y]` with integer coordinates, `-10^9 <= x, y <= 10^9` - The answer can exceed `2^31 - 1`: it is at most `n * (n - 1) / 2`, which is below `5 * 10^9`. Return it as an exact integer. - Checking every pair is too slow for the largest inputs; the intended running time is well below quadratic in `n`. ### Examples **Example 1** ```text Input: centers = [[0, 0], [2, 2], [3, 0], [5, 5]] Output: 2 ``` The squares centered at `(0, 0)` and `(2, 2)` touch at the corner `(1, 1)`, and the squares centered at `(2, 2)` and `(3, 0)` share part of the edge `y = 1`. No other pair meets. **Example 2** ```text Input: centers = [[1, 1], [1, 1], [1, 3]] Output: 3 ``` The two squares centered at `(1, 1)` coincide, and each of them touches the square centered at `(1, 3)` along the edge `y = 2`. **Example 3** ```text Input: centers = [[0, 0], [3, -1]] Output: 0 ``` The first square ends at `x = 1` and the second starts at `x = 2`, so they share no point.

Overview: A geometry counting problem: given integer centers of axis-aligned squares with side length 2, count the unordered pairs that overlap, where touching along an edge or at a single corner counts. It tests turning the geometry into a coordinate condition and counting pairs faster than a quadratic scan for up to 100,000 squares.

Read the full Capital One Machine Learning Engineer interview experience this question came from

You are given the centers of `n` axis-aligned squares on a plane, each with side length 2. The square centered at `(x, y)` covers every point `(u, v)` with `x - 1 <= u <= x + 1` and `y - 1 <= v <= y + 1`. Return the number of pairs of squares that overlap. Two squares overlap when they share at least one point, boundary points included, so squares that only touch along an edge or at a single corner count as overlapping. - Pairs are unordered and counted once: count index pairs `(i, j)` with `i < j`. - Centers may repeat, and squares with the same center overlap. - All squares are axis-aligned; none is rotated. The answer can exceed `2^31 - 1` (it is at most `n * (n - 1) / 2`, which is below `5 * 10^9`), so return it as an exact integer: `long` in Java and `long long` in C++. **Example 1** ```text Input: centers = [[0, 0], [2, 2], [3, 0], [5, 5]] Output: 2 ``` The squares centered at `(0, 0)` and `(2, 2)` touch at the corner `(1, 1)`, and the squares centered at `(2, 2)` and `(3, 0)` share part of the edge `y = 1`. No other pair meets. **Example 2** ```text Input: centers = [[1, 1], [1, 1], [1, 3]] Output: 3 ``` The two squares centered at `(1, 1)` coincide, and each of them touches the square centered at `(1, 3)` along the edge `y = 2`. **Constraints** - `1 <= n <= 10^5`, where `n = len(centers)` - `centers[i] = [x, y]` with integer coordinates, `-10^9 <= x, y <= 10^9` - The answer is at most `n * (n - 1) / 2 < 5 * 10^9` and can exceed `2^31 - 1`. - Checking every pair is too slow for the largest inputs; the intended running time is well below quadratic in `n`.

Constraints

  • 1 <= n <= 10^5, where n = len(centers)
  • centers[i] = [x, y] with integer coordinates, -10^9 <= x, y <= 10^9
  • Centers may repeat; squares with the same center overlap
  • The answer can exceed 2^31 - 1: it is at most n * (n - 1) / 2, which is below 5 * 10^9 (use long in Java, long long in C++)
  • Checking every pair is too slow for the largest inputs; the intended running time is well below quadratic in n

Examples

Input: ([[5, -7]],)

Expected Output: 0

Explanation: Minimum valid input: n = 1, a single square has no partner, so there are no pairs.

Input: ([[0, 0], [2, 0]],)

Expected Output: 1

Explanation: n = 2, centers 2 apart on x with equal y: the squares share the edge x = 1, and an edge touch counts as overlapping.

Hints

  1. Write the overlap condition for two squares in terms of their centers: when do the x-intervals [x - 1, x + 1] share a point, and when do the y-intervals?
  2. All coordinates are integers. For one fixed square, how many different center positions could another overlapping square possibly have?
  3. Centers can repeat many times. Make sure each unordered pair of indices is counted exactly once, including pairs of squares that share a center.

Count Pairs of Touching or Overlapping Side-2 Squares

hardCapital OneMachine Learning Engineer

You are given the centers of n axis-aligned squares on a plane, each with side length 2. The square centered at (x, y) covers every point (u, v) with x - 1 <= u <= x + 1 and y - 1 <= v <= y + 1.

Return the number of pairs of squares that overlap. Two squares overlap when they share at least one point, boundary points included, so squares that only touch along an edge or at a single corner count as overlapping.

  • Pairs are unordered and counted once: count index pairs (i, j) with i < j.
  • Centers may repeat, and squares with the same center overlap.
  • All squares are axis-aligned; none is rotated.

The answer can exceed 2^31 - 1 (it is at most n * (n - 1) / 2, which is below 5 * 10^9), so return it as an exact integer: long in Java and long long in C++.

Example 1

Input:  centers = [[0, 0], [2, 2], [3, 0], [5, 5]]
Output: 2

The squares centered at (0, 0) and (2, 2) touch at the corner (1, 1), and the squares centered at (2, 2) and (3, 0) share part of the edge y = 1. No other pair meets.

Example 2

Input:  centers = [[1, 1], [1, 1], [1, 3]]
Output: 3

The two squares centered at (1, 1) coincide, and each of them touches the square centered at (1, 3) along the edge y = 2.

Constraints

  • 1 <= n <= 10^5, where n = len(centers)
  • centers[i] = [x, y] with integer coordinates, -10^9 <= x, y <= 10^9
  • The answer is at most n * (n - 1) / 2 < 5 * 10^9 and can exceed 2^31 - 1.
  • Checking every pair is too slow for the largest inputs; the intended running time is well below quadratic in n.

Examples

Example 1
Input
([[5, -7]],)
Output
0
Notes
Minimum valid input: n = 1, a single square has no partner, so there are no pairs.
Example 2
Input
([[0, 0], [2, 0]],)
Output
1
Notes
n = 2, centers 2 apart on x with equal y: the squares share the edge x = 1, and an edge touch counts as overlapping.

Constraints

  • •1 <= n <= 10^5, where n = len(centers)
  • •centers[i] = [x, y] with integer coordinates, -10^9 <= x, y <= 10^9
  • •Centers may repeat; squares with the same center overlap
  • •The answer can exceed 2^31 - 1: it is at most n * (n - 1) / 2, which is below 5 * 10^9 (use long in Java, long long in C++)
  • •Checking every pair is too slow for the largest inputs; the intended running time is well below quadratic in n

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def count_overlapping_square_pairs(centers: list[list[int]]) -> int:
Sample call
count_overlapping_square_pairs([[5, -7]])
Language
Case 1
Input
([[5, -7]],)
Expected
0
Case 2
Input
([[0, 0], [2, 0]],)
Expected
1

Browse More Questions

Show the approach

Approach

Overlap test: the closed intervals [x1 - 1, x1 + 1] and [x2 - 1, x2 + 1] share a point exactly when |x1 - x2| <= 2, and two squares share a point exactly when their projections share a point on both axes. So squares i and j overlap if and only if |xi - xj| <= 2 and |yi - yj| <= 2 (boundary contact included).

Algorithm: group the centers by exact position in a hash map from (x, y) to its multiplicity c. Because coordinates are integers, every center that overlaps the square at (x, y) lies at (x + dx, y + dy) with dx, dy in [-2, 2]. Split the 24 non-zero offsets into 12 lexicographically positive ones (dx = 0 with dy in {1, 2}; dx in {1, 2} with dy in [-2, 2]) and their 12 negations. For each distinct position p with multiplicity c, add c * (c - 1) / 2 for the pairs of identical squares at p, then for each positive offset add c times the multiplicity found at p + offset.

Correctness: index pairs i < j with the same center are counted once by the c * (c - 1) / 2 term of that position. For two distinct positions p and q within the 5 x 5 block, exactly one of q - p and p - q is a positive offset, so the pair of positions is visited once and contributes c_p * c_q, which is the number of index pairs with one index in each group. Positions farther apart never overlap and are never visited. Summing gives exactly the number of overlapping index pairs.

Edge cases: n = 1 gives 0. All centers identical gives n * (n - 1) / 2, which exceeds 2^31 - 1 near the maximum n, so the total and every product use 64-bit integers (Java long, C++ long long; JavaScript numbers stay exact because the total is below 2^53). Coordinate differences reach 2 * 10^9, which overflows 32-bit subtraction; the references never subtract coordinates, and Java and C++ pack (x + 1000000002, y + 1000000002) into one 64-bit key whose parts each stay below 2^31, including neighbour positions up to 2 outside the coordinate range.

Time complexity:
O(n)
Space complexity:
O(n)