← Questions

Store words for lexicographic range queries

0
GoogleSystem designLast asked Oct 2025

A system design interview question asked in Google's system design round, last asked Oct 2025. It tests trees, indexing and storage.

Design storage for a persistent set of words that answers range queries: given [low, high], inclusive, return every stored word w with low <= w <= high, in dictionary order. Explain the data layout, the index and how a query runs, then how inserts, deletes and very large data are handled.

Requirements

  • Functional: query(low, high) -> list[str] in lexicographic order; insert(word) and delete(word) as a follow-up.
  • Non-functional: queries cost about log n plus the number of results; data may be larger than memory and must survive restarts.

For the words aa, aaa, ac, f, z:

query("a", "b")    ->  ["aa", "aaa", "ac"]
query("a", "aa")   ->  ["aa"]
query("b", "z")    ->  ["f", "z"]           # high is inclusive
query("ab", "b")   ->  ["ac"]

Clarifying questions

  • How many words, how long, and what alphabet (ASCII, Unicode, a collation order)?
  • Read-heavy, or are inserts frequent?
  • Are results usually small, or can one range return millions of words (paging)?
  • Is this one machine, or must it scale across machines?

What a strong answer covers

  • In memory: a sorted array with two binary searches (lower_bound(low), upper_bound(high)) when the set is static; a balanced tree or skip list when it changes. O(log n + k).
  • A trie: walk the common prefix of low and high, then output in order; when it beats a sorted structure and its memory cost.
  • On disk: a B+ tree or a sorted file of blocks with a sparse index of first keys per block, so a query reads the first block and then scans forward; prefix compression inside blocks.
  • Write-heavy data: an LSM tree (memtable, sorted runs, compaction) and merging runs at query time.
  • Scaling out: range-partition by key so a query touches few shards; split hot ranges; return results with a cursor for paging.
  • The comparison rule itself: byte order versus locale collation, and inclusive versus exclusive bounds.

Follow-ups

  • Return only the first k words of the range, then the next page.
  • Count the words in a range without listing them.
  • Support prefix queries ("all words starting with ab") with the same structure.

Where it's asked

  • Google · System design · Last asked Oct 202545 min · Whiteboard for the diagram, a doc for requirements and capacity math

What it tests

trees · indexing · storage

Your canvas
Draw your answer hereBoxes, arrows and notes, saved for you to come back to.Sign in to start drawing