Reported September 2024
ZipRecruiterhash table

Count Colliding Object Pairs

Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live ZipRecruiter OA. Under 2s to a working solution.
Founder's read

Two objects collide when both coordinate gaps are at most 2. That's the whole rule in this ZipRecruiter OA, reported in September 2024, and duplicates count too, so two objects sharing a center are a pair. The input is a list of [x, y] centers, up to 5000 of them, and you return the number of unordered index pairs as a long. It looks like a geometry question, but it's really a counting problem on a grid. If you blank under the timer, StealthCoder runs invisibly during the live assessment and gives you a working solution to check against.

The problem

Each row of centers contains integer coordinates [x, y] for one object; several objects may share a center.
Two objects collide exactly when both |x1 - x2| <= 2 and |y1 - y2| <= 2. Return the number of unordered colliding index pairs.

Function
countCollidingPairs(centers: int[][]) → long

Examples
Example 1
centers = [[1,1],[2,2],[0,4]]
return = 2
The first and second centers collide, and the second and third centers collide.
Example 2
centers = [[0,0],[0,0]]
return = 1
Objects sharing a center collide.

Constraints
0 <= centers.length <= 5000
Every row contains two integers.

Reported by candidates. Source: FastPrep

Pattern and pitfall

With n up to 5000, the brute-force double loop is about 12.5 million pair checks. That's fine and probably passes. It's the honest baseline, so write it first if you're short on time. The cleaner approach is hash-table counting. Store how many objects sit at each (x, y). For each distinct cell, add C(k,2) for same-cell pairs, then check neighbors in the 5x5 block around it. Only count each neighbor pair once, for example by only looking at cells that come later in a fixed ordering. The pitfalls are double counting unordered pairs, forgetting duplicates, and using int instead of long for the result. Empty input should return 0. If the neighbor ordering trips you up mid-assessment, StealthCoder is the hedge that gives you a verified version live.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Count Colliding Object Pairs cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass ZipRecruiter's OA.

ZipRecruiter reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Colliding Object Pairs FAQ

How hard is Count Colliding Object Pairs really?+

Easy to medium. The rule is simple, and the brute-force double loop works at 5000 objects. The difficulty is only in avoiding double counting and handling duplicate centers. If you can write nested loops with i < j, you have a correct answer.

What's the trick for this problem?+

The collision test is a Chebyshev distance of at most 2, so each object only interacts with a 5x5 block of cells. Count objects per coordinate in a hash map, then sum pairs within a cell and across neighboring cells, counting each pair once.

Is the O(n^2) brute force good enough?+

With n at most 5000, that's roughly 12.5 million comparisons, which is usually fine. Use i < j so each unordered pair is counted once. If you have time left, add the hash map version as an optimization, but a correct brute force beats a buggy clever one.

What edge cases should I test?+

Test an empty list, which should return 0. Test objects sharing the same center, as in [[0,0],[0,0]] returning 1. Test gaps of exactly 2 and exactly 3 on each axis. Negative coordinates should work too. Return a long so a large count of duplicates doesn't overflow.

How do I prepare for this in 48 hours?+

Practice grid-bucket counting with a hash map, and pair counting with k*(k-1)/2. Write the brute force from memory, then the neighbor-cell version. Check that both agree on the two given examples. That covers the pattern this ZipRecruiter question uses.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with ZipRecruiter.

OA at ZipRecruiter?
Invisible during screen share
Get it