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
- 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?
- All coordinates are integers. For one fixed square, how many different center positions could another overlapping square possibly have?
- Centers can repeat many times. Make sure each unordered pair of indices is counted exactly once, including pairs of squares that share a center.