</> MAANG.io
system design Β· 201

Intermediate

Master system design interviews with scalable architecture patterns, distributed systems, and real-world design challenges.

0/124 solved 0% complete

Quadtree

maang.io System Design Series


A quadtree recursively splits a 2D region into four quadrants, subdividing only where the data is dense. That structure turns "find everything near this point" and range queries from a full scan into a logarithmic descent β€” which is why quadtrees power maps, "nearby" search, collision detection, and image compression.

This deep dive comes in three parts:

  • Foundations & Core Concepts β€” recursive subdivision, node capacity, and point vs region quadtrees.
  • Internals β€” range and nearest-neighbour queries, the skew/degenerate cases, and comparisons to k-d trees and R-trees, with worked numbers.
  • Interview β€” Staff/Principal Q&A: choosing a spatial index and the trade-offs vs geohashing.

Quadtree β€” Part 2: Internals & Implementation

maang.io System Design Series


What is this?

Back to the dispatch office and its wall map full of pushpins. In Part 1 we learned the habit β€” cut a crowded district into four, recurse where it's still crowded. Now we're going to watch the clerk's hands: exactly what happens to the map when a new pin arrives, how the "cut into four" move re-files the existing pins, and how someone standing at the map answers "show me every pin within this rectangle" or "find the pin nearest to here" without scanning the whole wall.

We'll implement the PR (point-region) quadtree from Part 1 β€” equal-quarter splits, points in capacity-bounded leaves β€” because it's the one that powers find-nearby, collision detection, and mapping. By the end you'll be able to reason about its memory layout, its write and read paths, its exact cost with numbers, and the one input pattern that quietly destroys it.

The one-line idea: Every operation is "which of my four quadrants contains this point/rectangle?" applied recursively. Insert walks down to a leaf and splits it into four equal children when it overflows; a range query prunes any subtree whose box misses the query rectangle; and the whole thing is fast only as long as points are spread out enough that the tree stays shallow β€” which is exactly the assumption the degenerate case violates.


Sign in to continue reading

The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.

Sign in to read the full lesson

Sign in to MAANG.io

Use your Google or Microsoft account β€” no password to remember.

Continue with Google Continue with Microsoft

Please accept the terms above to continue.