Course contentsShow
← Computer Science
Lesson 407 of 2,87210. Hash TablesFree lesson

Hash Function Output Range and Table Size

How hash functions map to table indices and the relationship between output range and table capacity.

Hash Function Output Range and Table Size

What you'll learn: How a hash function's output range connects to the actual storage array in a hash table.


A hash function transforms a key (like a name or ID) into an integer, but that raw integer could be enormous—far larger than any practical storage array. The crucial question is: how do we convert that hash output into a valid array index?

The Output Range Problem

Think of a hash function like a lottery machine that spits out a random number between 0 and 999,999. But your storage "bin" only has 100 slots. You can't use the lottery number directly—you need to "squeeze" it down to fit your actual capacity.

Mapping to Table Indices

The standard technique uses the modulo operator (%). If your hash function produces a value hashValue and your table has tableSize slots, you compute:

index = hashValue % tableSize

This guarantees the result falls within [0, tableSize - 1]—perfect for array indexing.

The Relationship

  • Hash function output range: Can be huge (potentially billions of values)
  • Table size: Your actual array capacity (maybe 10, 100, or 1000 slots)
  • Modulo operation: The bridge that maps the large range down to valid indices

For example, if hashValue = 47382 and tableSize = 100, then:

index = 47382 % 100 = 82

The data goes into slot 82.

Why This Matters

A larger table size reduces collisions (different keys mapping to the same index) but uses more memory. A smaller table saves space but increases collision likelihood. The modulo step ensures every hash output, no matter how large, translates to a usable storage location.

Key Takeaway: Hash functions produce values in a potentially unlimited range, and we use the modulo operator with the table size to convert those values into valid array indices within our actual storage capacity.