Count Pairs of Overlapping Squares
Updated
Problem statement
Each entry centers[i] = [x, y] gives the integer coordinates of the center of one square. All squares have side length 2 and are aligned with the x and y axes, so the square centered at [x, y] covers every point from x - 1 to x + 1 horizontally and from y - 1 to y + 1 vertically, its border included.
Two squares overlap when some point lies in both of them. This includes squares that merely touch, whether along part of a side or at a single corner. Different entries may hold the same center. Such squares are identical, so any two of them overlap.
Return how many pairs of squares overlap. A pair is two different indices i < j, so each pair is counted once. The count can be larger than 2^31 - 1, so return it as a 64-bit integer.
Function
countOverlappingPairs(centers: int[][]) â longExamples
Example 1
centers = [[1,1],[2,3],[4,1],[5,0],[8,0]]return = 3[4,1]and[5,0]are 1 apart on each axis, so their squares share an area.[1,1]and[2,3]are 1 apart on x and 2 apart on y: the top side of the first square and the bottom side of the second share the segment from(1, 2)to(2, 2). Touching counts.[2,3]and[4,1]are 2 apart on each axis, so the squares meet at the single corner(3, 2). That counts too.- Every other pair is at least 3 apart on x, so there is a gap between the squares. The answer is
3.
Example 2
centers = [[3,-2],[3,-2],[3,-2],[3,1]]return = 3- The first three squares share one center, so they are the same square and each two of them overlap:
3pairs. - The square at
[3,1]is 3 above them, which leaves a gap. The answer is3.
Example 3
centers = [[0,0],[2,2],[4,4],[-2,2],[0,3]]return = 5- Corner contacts:
[0,0]with[2,2],[2,2]with[4,4], and[0,0]with[-2,2]. [0,3]is 2 apart on x and 1 apart on y from both[2,2]and[-2,2], so it touches each along a side.[0,3]is 3 above[0,0]and 4 away on x from[4,4];[-2,2]is 4 away from[2,2]on x. The answer is5.
Constraints
1 <= centers.length <= 8 * 10^4centers[i].length == 2-10^9 <= centers[i][0], centers[i][1] <= 10^9
More Capital One Online Assessment questions
- Distinct Values on Maximum-Sum FramesOA ¡ Seen Nov 2026
- Lowest Number in an Open RangeOA ¡ Seen Nov 2026
- Branch with the Most FilesOA ¡ Seen Nov 2026
- Earliest Meeting Time for All EmployeesOA ¡ Seen Nov 2026
- Earn the Most đOA ¡ Seen Nov 2026
- Rank Football TeamsOA ¡ Seen Oct 2026
- Count Subarrays Matching a Comparison PatternOA ¡ Seen Oct 2026
- Arithmetic Triplets After QueriesOA ¡ Seen Oct 2026
- Count One-Swap Number PairsOA ¡ Seen Oct 2026
- Sort Adjacent PairsOA ¡ Seen Oct 2026
Similar questions at other companies
- Bank Accounts with Balance HistoryMeta ¡ OAMeta interview questions
- Count Overlapping Square PairsRamp ¡ OARamp interview questions
- HTTP Request RedirectionAmazon ¡ OA
- Minimum Operations to Make a SubsequenceGoogle ¡ OAGoogle interview questions
- Count Distinct Tags in a Fixed-Radius Circle (Practice Reconstruction)Marshall Wace ¡ OAMarshall Wace interview questions
- Future-Effective Employee SalaryRippling ¡ ONSITE INTERVIEWRippling interview questions
- All Index Pairs Sum to KJP Morgan Chase ¡ PHONE SCREENJP Morgan Chase interview questions
- Versioned ArrayTrueFoundry ¡ PHONE SCREEN
- HTTP Log QueriesDatadog ¡ PHONE SCREEN ¡ ONSITE INTERVIEWDatadog interview questions
- Max Points on a LineLinkedIn ¡ ONSITE INTERVIEWLinkedIn interview questions
Capital One interview experiences
- Capital One Coding OA Questions: 3 Experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences