Detect Squares
DetectSquares - add points and count squares add(point): Add point to data structure (duplicates allowed) count(point): Count squares where this point is one corner
Detect Squares is a classic scan-and-record problem: the right bookkeeping beats brute force. Interviewers love it because it reveals your data-structure instincts and your edge-case discipline. The classic trap is mishandling duplicates or overwriting useful information too early.
Examples
You write
DetectSquares: add(point: List[int]), count(point: List[int])
Example 1
- Input
- add([3,10]), add([11,2]), add([3,2]), count([11,10])
- returns
- "1"
Example 2
- Input
- add([3,10]), add([11,2]), add([3,2]), count([14,8]), add([11,10]), count([11,10])
- returns
- "0,1"
The pattern behind it
Arrays & Hashing
I reach for it when
- You catch yourself scanning the array again for every element.
- The problem asks about duplicates, pairs, counts or groups.
Say this before you type. Say what the key is and what the value remembers, before you write the loop.
All 127 Arrays & Hashing problemsDesign
I reach for it when
- The statement describes a class with methods, not one function.
- Each method needs to be fast, and the structure decides how fast.
Say this before you type. Say each method, the cost you want from it, and the structure that earns that cost.
All 27 Design problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.