arXiv:2611.05235cs.DSNov 2026

Manuscript Offprint

arXiv:2611.05235

Catalan Hashing: Efficient Linear Probing for Variable-Length Items

Alex Conway, Hanna Komlós, William Kuszmaul

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

A defining feature of the linear-probing hash table is its spatial locality: at load factor 1−ε1 - \varepsilon, the hash table can be implemented so that each operation scans a single contiguous region of expected size O(1/ε)O(1/\varepsilon) (this is known as the scan cost). This guarantee, however, relies on all items having unit size. In this paper, we ask whether it is possible to achieve a similar guarantee, but in a hash table that stores items of arbitrary lengths ∣x∣≥1|x| \ge 1.

The main result of the paper is a new hash table called Catalan hashing, which supports amortized expected scan cost O(∣x∣/ε)O(|x|/\varepsilon) for positive queries, O(1/ε)O(1/\varepsilon) for negative queries, and O(∣x∣ε−1log⁡1+δ∣x∣)O(|x|\varepsilon^{-1}\log^{1+\delta}|x|) for insertions/deletions, for any constant δ>0\delta > 0. As a corollary, we obtain the same bounds in the cache-oblivious model, with a scan cost of SS translating to 1+O(S/B)1 + O(S/B) I/Os. Along the way, we introduce several high-level techniques that may be of independent interest; this includes a novel way of using erasure codes in order to perform write optimization within a hash table, and a connection to scheduling that allows us to place elements in a way which avoids clustering at high load factors.

1 Introduction

First introduced in the 1950s [48, 35], the linear-probing hash table has stood for more than half a century as one of the simplest and most widely used hash table designs. In its most basic form, the hash table works as follows. Elements live in an array of some size mm, and each element xx hashes to a uniformly random position h(x)h(x) in that array. To insert an item xx, we examine cells h(x),h(x)+1,h(x)+2,…(modm)h(x), h(x)+1, h(x)+2, \ldots \pmod{m} until we find a free cell where we place the item; and, likewise, to query an item xx, we scan the cells h(x),h(x)+1,h(x)+2,…(modm)h(x), h(x)+1, h(x)+2, \ldots \pmod{m} and until we either find xx, or until we encounter a free cell (meaning that xx is not present).

What makes linear probing special—both in practice and in theory—is its spatial locality [37, 44, 14, 16, 24, 50]: each operation accesses just one small contiguous region of the hash table. We refer to the size of this region as the scan cost of the operation. (This quantity has also been called the locality of the operation [57]).

At constant load factor 1−Θ(1)1-\Theta(1), the expected scan cost per operation is O(1)O(1). When the hash table is filled to a high load factor 1−ε1-\varepsilon for a small parameter ε∈(0,1)\varepsilon \in (0,1), naive linear probing suffers from a phenomenon known as primary clustering [35, 38, 37], which increases the worst-case expected scan cost to Θ(ε−2)\Theta(\varepsilon^{-2}). Fortunately, this clustering effect can actually be eliminated with the help of additional algorithmic optimizations (the use of the Robin-Hood ordering to improve queries [3, 19] and the use of graveyard hashing [14] to improve insertions). With these optimizations in place, the expected scan cost per operation is O(ε−1)O(\varepsilon^{-1}).

The above results all operate under the assumption that items have a fixed size. In many applications, however, this is not the case: An item xx may occupy an arbitrary number ∣x∣≥1|x| \geq 1 of words (representing, for instance, a variable-length key together with a variable-length value). A common workaround is to use indirection: store a fixed-size pointer in the hash table and allocate the item itself elsewhere in memory. But indirection defeats the very spatial locality that makes linear probing so desirable—each operation must now access multiple separate regions of memory.

This raises a natural question: Can linear probing be adapted to store variable-length items inline, so that the expected scan cost per query is close to O(ε−1⋅∣x∣)O(\varepsilon^{-1} \cdot |x|), where ∣x∣|x| is the length of the item being queried? In other words, does there exist a data structure that behaves like linear probing, but that supports items of arbitrary sizes?

Such a data structure would also be of direct interest in the external-memory model [1], where memory is divided into blocks of size BB and the cost of an operation is the number of blocks it accesses (called I/OsI/Os). In this model, a linear-probing operation with scan cost SS incurs 1+O(S/B)1+O(S/B) I/Os in expectation, so bounds on scan cost translate directly into bounds on I/Os. Thus, another way to frame our question is simply: does there exist a cache-oblivious1^1 external-memory data structure in which querying an item xx costs 1+O(ε−1∣x∣/B)1+O(\varepsilon^{-1}|x|/B) I/Os in expectation?

This Paper: Catalan Hashing. In this paper, we introduce Catalan hashing, which successfully extends linear probing to support variable-length items. In the final version of the data structure (Theorem 1.4), we are able to support queries with an amortized expected scan cost of

O(ε−1⋅∣x∣),O(\varepsilon^{-1} \cdot |x|),

and insertions and deletions with an amortized expected scan cost of

O(ε−1⋅∣x∣⋅log⁡1+δ∣x∣),O(\varepsilon^{-1} \cdot |x| \cdot \log^{1+\delta} |x|),

where δ>0\delta > 0 is a positive constant of our choice. The query bound applies to both positive queries (for elements that are present) and negative ones (for elements that are absent). In fact, for negative

1^1A data structure is cache-oblivious if it does not need to know the block size BB or cache size MM in advance.

queries, we are able to achieve an even stronger expected scan-cost bound of O(ε−1)O(\varepsilon^{-1}), independent of the item size ∣x∣|x|.

We develop our results systematically in five parts. Parts 1–3 cover static and then dynamic constructions in the constant-load-factor regime (load factor 1−Θ(1)1 - \Theta(1)). Parts 4–5 then extend these constructions to the high-load-factor regime (load factor 1−ε1 - \varepsilon). We state our results in terms of scan cost, which implies corresponding I/O bounds. We state these explicitly here for our final construction and following the theorems in the main text.

Part 1: Simple Catalan Hashing (Static, Low Load Factor). A naive way to store items inline in a linear-probing hash table would be to store each item xx as a contiguous block of ∣x∣|x| words starting at or after its hash location h(x)h(x). However, as observed by Thorup [54], this can blow up the cost of small items: a single large item of length LL occupies LL consecutive cells, so an item of length 1 that hashes into that interval is pushed Ω(L)\Omega(L) cells away from its hash location.

In Part 1 (Sections 3 and 4), we introduce (Simple) Catalan hashing, a static linear probing hash table (designed for low load factors 1−Θ(1)1 - \Theta(1)) that resolves the problem described above. The basic idea is to store items non-contiguously, nesting smaller items inside larger ones. We then augment each cell of the table to store an O(1)O(1)-bit tag indicating where items begin and end. The items are laid out in such a way that, whenever a smaller item xx overlaps a larger one yy, xx is contained entirely within yy. If one thinks of each tag as encoding either an open parenthesis (starting a new item) or a closing parenthesis (ending the current item), then the tags form a balanced-parentheses sequence (i.e., a Catalan sequence [53]), hence the name of the data structure.

The main result of Part 1 is the following theorem:

Theorem 1.1 (Simple Catalan Hashing). Let SS be a static set of variable-length items stored in an mm-cell table at load factor load(S)=1m∑y∈S∣y∣≤1−Ω(1)S) = \frac{1}{m} \sum_{y \in S} |y| \leq 1 - \Omega(1). Simple Catalan hashing uses O(1)O(1) bits of metadata per cell, answers positive queries for any item x∈Sx \in S with expected scan cost O(∣x∣)O(|x|), and answers negative queries with expected scan cost O(1)O(1).

Note that the bound for negative queries (queries to keys that are absent) does not even depend on the length ∣x∣|x| of the item being queried. Negative queries make use of the following mechanism: If an item xx hashes to some cell cc, and xx is the last item in the table with that hash, then at the end of xx we store an O(1)O(1)-bit tag that indicates this. A negative query for a key kk with hash cc can therefore stop as soon as it reaches the tag at the end of xx. Of course, if xx is very long, then this could make the query very expensive! Critically, however, long items are necessarily rare (the sum of the lengths of all items is at most the size of the table), so a negative query is unlikely to share its hash with that of a long item. Summing over the items in the table, we show that the expected scan cost of a negative query is just O(1)O(1).

Part 2: Write Optimization via Erasure Codes (Dynamic, Low Load Factor). Next, in Part 2, we turn to the question of how to support insertions (deletions are deferred to Part 5). Note that, here, we are still only focusing on hash tables that operate at low load factors—we consider higher load factors in Parts 4 and 5.

Note that naively dynamizing the simple Catalan hash table results in severe write amplification (insertions are very expensive). Suppose, for example, that the table stores a long item yy of length LL, and we insert an item xx of length 1 whose hash lands in the middle of yy. In the static layout, xx interrupts yy, so inserting xx requires shifting the remaining Ω(L)\Omega(L) words of yy forward, giving the insertion a scan cost of Ω(L)\Omega(L). Worse, if yy is itself nested inside an even longer item y′y', then shifting yy forces y′y' to shift as well, triggering a cascade of updates across items of increasing size.

In the external-memory literature, the classic way to resolve write amplification is to make the data structure write optimized [42, 4, 18, 9, 29, 10, 21]. However, existing write-optimized data structures (such as BεB^{\varepsilon}-trees [18], LSM-trees [42], cache-oblivious streaming B-trees [9], etc.) rely on hierarchical trees of buffers to batch small updates as they trickle down the tree. In a linear-probing hash table, however, there is no tree, and no natural way to add hierarchical buffers in a way that respects spatial locality. Thus, a priori, write optimization would seem to be off the table.

Our solution is a new mechanism for write optimization in linear-probing hash tables: using erasure codes so that large items can themselves act as “buffers” for small items. Instead of storing an item yy of length s=∣y∣s = |y| directly, we encode yy using an erasure code Enc(y)\text{Enc}(y) of length 2s2s from which yy can be reconstructed using any ss surviving symbols. When a newly inserted short item xx needs a cell that is currently occupied by a longer element yy, xx does not move yy at all—it simply overwrites yy's cell in place. As long as at least ss of the 2s2s cells of Enc(y)\text{Enc}(y) survive, any query for yy can still decode yy in a single contiguous scan, just as before. Only when half of Enc(y)\text{Enc}(y) has been overwritten do we reinflate yy by reinserting it as a fresh length-2s2s codeword; critically, at that point, at least s=∣y∣s = |y| words of shorter items have been written on top of yy, which is enough to amortize the cost of reinflating yy.

To illustrate the high-level idea behind this technique, we begin in Section 5 with a warmup in which we analyze this erasure-coded Catalan hash table under the simplifying assumption that an erasure oracle notifies the algorithm whenever half of an item's codeword has been overwritten (and supplies the original item to be reinflated). Under this oracle assumption, we prove (Theorem 5.5) that positive queries and insertions both have amortized expected scan cost O(∣x∣)O(|x|).

Part 3: Removing the Oracle (Dynamic, Low Load Factor). In Part 3 (Section 6), we show how to remove the oracle assumption from Part 2. What makes removing the oracle nontrivial is that, when a short insertion overwrites a cell belonging to a long item yy, the algorithm cannot, for example, afford to update any counter that is stored at the start of yy. Because yy may be very long, the start of yy may lie far before the cell being overwritten, so updating a per-item counter on every overwritten cell would destroy our O(∣x∣)O(|x|) bound on the scan cost of an insertion. This makes it difficult to identify when an item yy has had more than half of its codewords overwritten.

Our solution is to do nothing at all to yy when its cells are overwritten, and instead reinflate heavily overwritten items lazily during queries. Whenever an item xx is inserted, we also store a backup copy of xx in a secondary backup table B\mathcal{B} that is interleaved into the same physical array. Most queries for an item yy simply find at least ∣y∣|y| surviving symbols of Enc(y)\text{Enc}(y) in the primary table and decode yy with scan cost O(∣y∣)O(|y|). If, however, more than half of Enc(y)\text{Enc}(y) has been overwritten, the query falls back to recovering yy from the backup table B\mathcal{B} and reinflates Enc(y)\text{Enc}(y) in the primary table.

At first glance, introducing B\mathcal{B} might appear to be circular, since B\mathcal{B} must itself dynamically store variable-length items. The crucial difference is that a query for yy only accesses B\mathcal{B} when at least ∣y∣|y| cells of Enc(y)\text{Enc}(y) have been overwritten since yy was last inserted or reinflated. We can therefore amortize the cost of querying B\mathcal{B} (and reinflating yy) against the insertions that caused those overwrites. Critically, this means that we can get away with B\mathcal{B} supporting slower queries than what we want in the full data structure—it just needs to support all operations with scan cost O(∣x∣log⁡1+δ∣x∣)O(|x|\log^{1+\delta}|x|). The final contribution of Section 6 is to construct a backup table that does just that. Integrating this into the full data structure gives the following theorem:

Theorem 1.2 (Dynamic Catalan Hashing at Low Load Factor). Fix any constant δ>0\delta > 0. There is an insertion-only dynamic Catalan hash table that supports some constant load factor Ω(1)\Omega(1), and that answers positive queries for any item xx with amortized expected scan cost O(∣x∣)O(|x|), answers negative queries with expected scan cost O(1)O(1), and supports insertions of any item xx with amortized expected scan cost O(∣x∣log⁡1+δ∣x∣)O(|x|\log^{1+\delta}|x|).

Part 4: Fair Catalan Hashing (Static, High Load Factors). In Part 4 (Section 7), we move from constant load factors to the high-load-factor regime (load factor 1−ε1 - \varepsilon for some small ε∈(0,1)\varepsilon \in (0,1)). To tackle this regime, we begin by returning to the static setting.

At high load factors, simple Catalan hashing ends up encountering the same quadratic barrier as classical linear probing—the worst-case expected scan cost for an item xx becomes Θ(ε−2∣x∣)\Theta(\varepsilon^{-2}|x|). The standard fix in the unit-size setting is to use the Robin Hood hashing [3, 19], which sorts elements in each run in increasing order of hash. But, for variable-length items, this approach fails, because a single long item can blow up the query time of many subsequent short items with larger hashes.

To overcome this obstacle, we establish a connection between variable-length linear probing and preemptive single-server scheduling [51, 33, 32, 6, 5, 26, 28]. If we view each item x as a job of size ∣x∣|x| arriving at time h(x)h(x), then we can reinterpret the task of placing the items into the table as a scheduling problem. Under this view, the scan cost of an item x divided by its size ∣x∣|x| is the same as the stretch of the corresponding job.

What we want, then, is a scheduling policy that is fair across all job sizes, guaranteeing worst-case expected stretch O(ε−1)O(\varepsilon^{-1}). In queuing theory, the simplest such policy is last-in-first-out with preemption, in which newly-arrived jobs immediately preempt any existing jobs. In our hash-table setting, this policy can be viewed as giving priority to items with larger hashes. Note that this prioritization is the exact opposite of the classical Robin Hood ordering [3, 19]. We call the resulting data structure anti-Robin-Hood Catalan hashing (ARCH).

In Section 7, we give a simple, self-contained proof that the ARCH table does, indeed, guarantee an expected scan length of at most ε−1∣x∣2\varepsilon^{-1}|x|^{2}. Perhaps ironically, this means that the anti-Robin-Hood ordering is actually fairer than its Robin Hood counterpart, once we allow for variable-length items.

Theorem 1.3 (Anti-Robin-Hood Catalan Hashing). Let ε∈(0,1)\varepsilon \in (0,1), and let SS be a static set of items stored in an mm-cell table with load factor load(S)≤1−ε(S) \leq 1 - \varepsilon. Anti-Robin-Hood Catalan hashing (ARCH) uses O(1)O(1) bits of metadata per cell, answers positive queries for any item x∈Sx \in S with expected scan cost at most ∣x∣/ε|x|/\varepsilon, and answers negative queries with expected scan cost at most 1+1/ε1 + 1/\varepsilon.

We remark that, although the anti-Robin-Hood ordering bounds the expected stretch of every item, which is what we need in Part 5, its expected maximum stretch can be quite large: if the table contains many items of length 2 and a single item of length LL, then with constant probability the long item preempts one of the short items, giving that item stretch Ω(L)\Omega(L). As an additional result, we show how to bound the expected maximum stretch as well. We adapt the fair-sojourn scheduling protocol of Friedman and Henderson [26] to design a variant of Catalan hashing with expected scan cost at most 1+∣x∣/ε1 + |x|/\varepsilon and expected maximum stretch at most 1+(ln⁡n+1)/ε1 + (\ln n + 1)/\varepsilon (Theorem 7.5).

Part 5: Dynamic Catalan Hashing at High Load Factors. Finally, in Part 5 (Section 8), we combine our static ARCH construction from Part 4 with our dynamic low-load-factor construction from Parts 2 and 3 to obtain a fully dynamic Catalan hash table at load factor 1−ε1 - \varepsilon (supporting both insertions and deletions).

Note that the dynamic erasure-coded hash table from Parts 2 and 3 cannot be run directly at load factor 1−ε1 - \varepsilon: its write-optimization machinery inherently loses a constant factor in space. Conversely, the scheduling-based approach from Part 4 achieves load factor 1−ε1 - \varepsilon with only O(1)O(1) bits of metadata per cell, but is inherently static.

To get the best of both worlds, we use a structure inspired by graveyard hashing [14]. At a high level, we store almost all of the items in a static ARCH table Hfull\mathcal{H}_{\text{full}} at load factor 1−O(ε)1 - O(\varepsilon), while routing new insertions into a much smaller dynamic erasure-coded table Hsmall\mathcal{H}_{\text{small}} of size Θ(εm)\Theta(\varepsilon m) that operates at constant load factor. Because Hsmall\mathcal{H}_{\text{small}} is only a Θ(ε)\Theta(\varepsilon) fraction of the size of Hfull\mathcal{H}_{\text{full}}, its constant-factor space overhead consumes only O(εm)O(\varepsilon m) cells in total. Moreover, so that an operation

2^{2}It is worth noting that this bound holds not just asymptotically but exactly.

that accesses both tables still scans just one small region of memory, we interleave Hsmall\mathcal{H}_{\mathrm{small}} uniformly into Hfull\mathcal{H}_{\mathrm{full}} within a single physical array—placing one slot of Hsmall\mathcal{H}_{\mathrm{small}} every Θ(ε−1)\Theta(\varepsilon^{-1}) slots of Hfull\mathcal{H}_{\mathrm{full}}—and use a shared hash function h(x)h(x) for both tables. That way, any operation that probes both Hfull\mathcal{H}_{\mathrm{full}} and Hsmall\mathcal{H}_{\mathrm{small}} still traverses a single contiguous region of memory around h(x)h(x) (stretching the physical scan cost of Hsmall\mathcal{H}_{\mathrm{small}} by a factor of Θ(ε−1)\Theta(\varepsilon^{-1})).

Insertions go directly into Hsmall\mathcal{H}_{\mathrm{small}}, and a query simply searches both tables. After every Θ(εm)\Theta(\varepsilon m) words of insertions, we perform a global rebuild that merges Hsmall\mathcal{H}_{\mathrm{small}} into Hfull\mathcal{H}_{\mathrm{full}} using O(1)O(1) sequential passes over the table. Because the rebuild occurs only once every Θ(εm)\Theta(\varepsilon m) words of updates, its O(m)O(m) cost amortizes to O(ε−1)O(\varepsilon^{-1}) per inserted word—or O(∣x∣/ε)O(|x|/\varepsilon) for an item xx. To support deletions without making negative queries depend on the key length, we keep constant-size fingerprints of deleted static keys in another interleaved table, checking for collisions during rebuilding. The details, together with the low-load deletion mechanism, appear in Section 8 and Appendix C. Putting these pieces together gives our main result:

Theorem 1.4 (Dynamic High-Load Catalan Hashing). Let ε∈(0,1/2)\varepsilon \in (0,1/2) and let δ>0\delta > 0 be a constant. There is a dynamic Catalan hash table operating at load factor 1−ε1 - \varepsilon that supports positive queries for any item xx with amortized expected scan cost

O(∣x∣ε),O\left(\frac{|x|}{\varepsilon}\right),

supports negative queries with amortized expected scan cost O(1/ε)O(1/\varepsilon), and supports insertions and deletions of any item xx with amortized expected scan cost

O(∣x∣log⁡1+δ∣x∣ε).O\left(\frac{|x|\log^{1+\delta}|x|}{\varepsilon}\right).

As an immediate corollary we also get the following result in the external memory model, establishing the existence of a linear-probing-like data structure for items of arbitrary sizes.

Corollary 1.5 (Dynamic High-Load Catalan Hashing in External Memory). Suppose M≥CM(B+smax⁡ε−2log⁡3m)M \geq C_M(B + s_{\max}\varepsilon^{-2}\log^3 m) for a sufficiently large constant CMC_M, where smax⁡s_{\max} is the maximum length of any item stored or inserted during the operation sequence. The hash table of Theorem 1.4 is cache-oblivious and, for every block size BB, supports positive queries for any item xx in amortized expected

1+O(∣x∣εB)1+O\left(\frac{|x|}{\varepsilon B}\right)

I/OsI/Os, supports negative queries in amortized expected 1+O(1/(εB))1+O(1/(\varepsilon B)) I/OsI/Os, and supports insertions and deletions of any item xx in amortized expected

1+O(∣x∣log⁡1+δ∣x∣εB)1+O\left(\frac{|x|\log^{1+\delta}|x|}{\varepsilon B}\right)

I/OsI/Os.

Paper Outline. The rest of the introduction is spent discussing in more detail related work on linear probing and on external-memory data structures. Then, after preliminaries in Section 2, we introduce the basic machinery that all of our static constructions share in Section 3, and then we complete Parts 1–5 in Sections 4 to 8, respectively.

References (53)