Some problems are fundamentally about relationships between numbers or nodes.
Others are fundamentally about relationships between prefixes — which words share a beginning, which strings could still match a partially typed query, which substring shows up buried inside a much longer one. For that second category, a hash set or a sorted list will get you a working answer, but it will rarely get you the efficient answer, and it will almost never get you the elegant one. The trie (pronounced “try,” from retrieval, though most engineers just say “tree”) is the data structure built specifically for prefix relationships, and rolling-hash techniques like Rabin-Karp are the corresponding tool for fast substring search. this article covers both.
The signal for reaching for a trie is fairly distinctive once you’ve seen it a few times. If a problem talks about autocomplete, typeahead, or “as the user types,” that’s a trie. If it asks you to check membership against a large, fixed dictionary of words — especially if you’ll be doing many such checks against the same dictionary — a trie beats a hash set because it lets you bail out early on impossible prefixes and lets you search a grid or a graph without ever wasting time exploring a path that no dictionary word could produce. If a problem mentions “shared prefix” explicitly, or if it describes matching against IP address ranges, file paths, or URL routes, that’s also trie territory: hierarchical, prefix-structured data is what a trie is for. Conversely, a trie is the wrong tool if the words in your dictionary don’t share meaningful prefix structure, if you only need a handful of lookups against a small set (a plain hash set is simpler and just as fast in practice), or if you need to check whether a string exists as a subsequence rather than a prefix — that’s a different technique entirely.
Rabin-Karp and its rolling-hash relatives solve an adjacent but distinct problem: finding every occurrence of a needle string somewhere inside a haystack string, not just testing prefixes of a dictionary. The signal here is “find all positions where this string occurs inside a longer string” combined with a hint that naive character-by-character comparison would be too slow. It’s less commonly asked as a full implementation exercise than a trie is, but interviewers like it because it reveals whether you understand hashing beyond hash maps — whether you can build one from first principles and reason about its collision behavior.
Both structures exist to avoid a specific waste: redoing the same character comparisons over and over across many strings that share a common beginning, or across many overlapping windows of the same string. Once you see that shared cost, the data structures practically design themselves.
Continue with the book
Explore the full treatment, worked examples, and exercises in Algorithmic Edge.
Companion article for Algorithmic Edge, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.
