KatInterview

Exact Top-1,000 Terms Across a Petabyte of Text Files

System DesignModel answer
MediumModel answerSystem Designdistributed-systemsmapreducefault-tolerance

Last updated

Design a distributed batch pipeline that exactly counts term occurrences across petabytes of immutable text and returns the 1,000 most common, handling skew, split edges and retries.

Exact Top-K Term Counts at Petabyte Scale

You have a very large corpus of plain-text files, around a petabyte in total, sitting in an immutable object store such as S3. Individual files can be arbitrarily large. Design a distributed batch system that produces the 1,000 most frequent words across the whole corpus, together with their counts.

Requirements

Functional

  • Accept files of any size.
  • Counts must be exact. Probabilistic sketches (Count-Min, etc.) are not allowed.
  • Processing must be idempotent, so that worker crashes and retries do not change the result.

Non-functional

  • The output must be deterministic, independent of the order in which parallel tasks finish.
  • The design must scale horizontally across a cluster.
  • Extremely common words (for example "the") must not overload any single aggregation task.

What to cover

  1. Architecture: the stages from reading input through tokenizing, aggregating and producing the final ranking.
  2. Splitting: how you cut large files into pieces without losing or double-counting words that straddle a cut.
  3. Data model: what workers exchange with each other, with the coordinator and with storage.
  4. Failure handling: what happens when a worker dies in the middle of a file segment.

State your assumptions about scale and the definition of a "word" before designing.

Hints

Hint 1

Frame it as a map, shuffle, reduce job. Think about what each map task can pre-aggregate locally before sending anything over the network.

Hint 2

To avoid splitting words at chunk edges, define an ownership rule based on where a word begins, so every word belongs to exactly one chunk.

Hint 3

Make every task a pure function of immutable input, write output to an attempt-specific location, and publish it with a first-writer-wins commit.

Hint 4

For top-K, ask whether a word's total can ever be spread over several reducers. If each word is routed to exactly one reducer, local top-K lists are enough to build the global top-K.