Detect Squares
A medium-tier problem at 51% community acceptance, tagged with Array, Hash Table, Design. Reported in interviews at Google and 0 others.
Detect Squares is a medium-difficulty design problem that Google has asked repeatedly in coding interviews. You're given a stream of points and need to detect axis-aligned squares on the fly. Fifty percent of candidates get stuck because they confuse it with a geometry problem when it's actually a counting and hash table problem. You need to track points efficiently, then for each new point, check if it can complete a square with three points you've already seen. If you hit this during your OA and blank on the counting trick, StealthCoder surfaces the solution in seconds, invisible to the proctor.
Companies that ask "Detect Squares"
Detect Squares is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage.
Get StealthCoderThe pattern: store all points in a hash table with their frequencies. When a new point arrives, iterate through all previously seen points and check if they form a diagonal of a potential square (same distance from the new point in both x and y directions). For each valid diagonal, you need the other two corners to exist in your hash table. The naive approach tries to enumerate all squares from scratch each time, which is slow. The trick is realizing that for a new point (x1, y1) and an existing point (x0, y0), if abs(x1 - x0) == abs(y1 - y0) and both are nonzero, they could be opposite corners of a square. Then you just verify the other two corners exist in your hash table. Hash table lookups make this O(n) per new point instead of O(n^2). StealthCoder locks in this solution pattern when you freeze on the geometry aspect.
Pattern tags
You know the problem.
Make sure you actually pass it.
Detect Squares recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Detect Squares interview FAQ
Is Detect Squares actually asked at Google, or is it rare?+
Google reports this one. It's not in every loop, but it shows up enough that prep should include it. The design and hash table elements make it a Google-style filter question, so expect it if you interview there.
What's the trick I'm missing if I think it's just a geometry problem?+
Most people try to compute distances or slopes and get bogged down. The real trick is flipping it: for each new point, only check points that could form a valid diagonal, compute the two missing corners, and do a hash table lookup. Geometry is the setup, not the solution.
How does hash table counting apply here?+
You store points and their counts. When checking if a square is valid, you multiply the counts of the two missing corners to get the number of squares that new point completes. If a corner has been added twice, it counts twice in the product.
What's the time complexity per operation?+
Adding a point is O(1) hash insertion. Querying a point iterates through all points you've stored so far to find diagonals, so O(n) per query in the worst case. Space is O(n) for the hash table.
Does Detect Squares test the same skills as other hash table design problems?+
Yes and no. Hash table plus design is common, but this one layers in geometry logic as a distraction. Other hash table problems test collision handling or frequency counting. This one tests whether you can see the counting angle inside a geometry frame.
Want the actual problem statement? View "Detect Squares" on LeetCode →