Implement a QuadTree for 2D Spatial Indexing with Range Count Queries
Company: Miro
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a QuadTree for 2D spatial indexing over an axis-aligned bounded region `[x1, y1, x2, y2]`.
Each node holds up to `capacity` points. When a node overflows, it subdivides into four equal quadrants (NW, NE, SW, SE) and redistributes its points.
The structure must support inserting points, counting points within an axis-aligned range query, and reporting the total number of stored points. Coordinates are integers and every bound is inclusive, so a point on the edge of the region is inside it. The same point may be inserted more than once; each successful insert counts toward `size` and toward every query that covers the point.
Implement a `QuadTree` class, constructed as `QuadTree(x1, y1, x2, y2, capacity)`, with the following methods:
- `insert(x: int, y: int) -> bool`: Insert point (x, y) into the QuadTree. Returns True if inserted, False if the point lies outside the root bounds.
- `queryRange(x1: int, y1: int, x2: int, y2: int) -> int`: Return the count of inserted points contained in the axis-aligned rectangle [x1, y1, x2, y2], bounds inclusive. The rectangle may extend past the root region.
- `size() -> int`: Return the total number of points currently stored in the QuadTree.
Example 1: a split at the root.
```text
Input:
["QuadTree", "insert", "insert", "insert", "insert", "queryRange", "queryRange", "size"]
[[0, 0, 10, 10, 2], [1, 1], [2, 2], [8, 8], [9, 9], [0, 0, 5, 5], [0, 0, 10, 10], []]
Output: [null, true, true, true, true, 2, 4, 4]
Explanation:
qt = QuadTree(0, 0, 10, 10, 2) # bounds [0, 0, 10, 10], capacity 2
qt.insert(1, 1) # -> True (stored at the root)
qt.insert(2, 2) # -> True (the root is now full)
qt.insert(8, 8) # -> True (the root splits; (1, 1) and (2, 2) move to SW, (8, 8) goes to NE)
qt.insert(9, 9) # -> True (stored in NE)
qt.queryRange(0, 0, 5, 5) # -> 2 ((1, 1) and (2, 2))
qt.queryRange(0, 0, 10, 10) # -> 4 (all points)
qt.size() # -> 4
```
Example 2: negative coordinates and a point outside the region.
```text
Input:
["QuadTree", "insert", "insert", "insert", "queryRange", "queryRange", "size"]
[[-5, -5, 5, 5, 1], [0, 0], [-3, -3], [100, 100], [-5, -5, 0, 0], [-10, -10, 10, 10], []]
Output: [null, true, true, false, 2, 2, 2]
Explanation:
qt = QuadTree(-5, -5, 5, 5, 1) # bounds [-5, -5, 5, 5], capacity 1
qt.insert(0, 0) # -> True
qt.insert(-3, -3) # -> True (forces subdivision)
qt.insert(100, 100) # -> False (outside the root bounds)
qt.queryRange(-5, -5, 0, 0) # -> 2 ((0, 0) and (-3, -3))
qt.queryRange(-10, -10, 10, 10) # -> 2 (the query covers the whole region)
qt.size() # -> 2
```
```hint Splitting a node
Choose the midpoint and the child ranges so that every integer point of the parent belongs to exactly one child. Test your midpoint formula on a box with negative coordinates.
```
```hint Points that cannot be separated
Trace your insert when capacity + 1 points share the same coordinates, and make sure it stops.
```
### Constraints & Assumptions
- `-10⁶ <= x1 < x2 <= 10⁶` and `-10⁶ <= y1 < y2 <= 10⁶` for the root region
- `1 <= capacity <= 16`
- At most `10⁴` calls in total to `insert` and `queryRange`
- Points outside the root bounds are ignored: `insert` returns `False` and stores nothing.
### Clarifying Questions to Ask
- Will range queries far outnumber inserts, and do callers need only counts or the points themselves?
- Will delete or move operations be needed later, and should emptied quadrants merge back into one leaf?
- If a query arrives with its corners in the wrong order (x1 > x2), should it be normalized or treated as empty?
### What a Strong Answer Covers
- A node layout (bounds, stored points, four children) and a split rule under which the children cover every integer point of the parent exactly once, including with negative coordinates.
- An insert that walks to the correct leaf, splits it on overflow, moves the old points down, and still terminates when many points share one location.
- A range count that skips nodes outside the rectangle, uses inclusive comparisons, and avoids walking subtrees it does not need to open.
- Time and space complexity in terms of tree depth, capacity and point count, including the worst case for clustered points.
- Boundary cases: points on the root edge, inserts outside the root, and queries that reach past it.
### Follow-up Questions
- How would you support deleting a point, and when would you merge four children back into one leaf?
- How would you return the points in the rectangle, or the k nearest points to a location, instead of a count?
- The stored items are rectangles of different sizes (shapes on a canvas) instead of points. How does the structure change, and when would an R-tree or a uniform grid be a better fit?
- What changes if coordinates are floating-point numbers?
Overview: Implement a quadtree over a bounded integer region that inserts points, splits a node when it overflows, and counts the points inside an inclusive query rectangle. It tests exact integer splitting with negative coordinates, handling of repeated points, query pruning and complexity analysis.