Data Structures
Hash tables from scratch, resolving collisions to building dynamic maps
Learn how hash tables map arbitrary keys to array indices, handle collisions gracefully, and resize dynamically for constant-time lookups.
Plain words. One step at a time. Nothing here assumes you already know the idea.
The idea
A hash table is a data structure that stores associative key-value pairs by using a mathematical function to compute an index from each key into an array of buckets. This structure allows programs to insert, retrieve, and delete values in constant average time.
Imagine a wall of post-office mailboxes, where each mailbox has a fixed numeric slot number.
You will need. Basic knowledge of arrays and indexing · Understanding of modulo arithmetic · Familiarity with writing functions and classes in Python
One pass
Step 1 of 4
An array lets you retrieve any element immediately if you know its numerical index. However, programs often need to look up data using non-numeric labels such as usernames or email addresses. A hash table bridges this gap by translating non-numeric keys into valid array indices.
Smallest version
A minimal fixed-size hash table without collision resolution
class MinimalHashTable:
def __init__(self, size=10):
self.size = size
self.slots = [None] * size
def _hash(self, key):
total = sum(ord(char) for char in str(key))
return total % self.size
def set(self, key, value):
index = self._hash(key)
self.slots[index] = (key, value)
def get(self, key):
index = self._hash(key)
pair = self.slots[index]
if pair is not None and pair[0] == key:
return pair[1]
return None
# Demonstration
table = MinimalHashTable(size=5)
table.set("apple", 42)
table.set("banana", 99)
assert table.get("apple") == 42
assert table.get("banana") == 99
assert table.get("cherry") is NoneKeep these
- Hash tables use hash functions to convert arbitrary keys into integer indices.
- The modulo operator bounds the resulting integer to the size of the backing array.
- Keys that produce identical indices will overwrite each other unless collisions are handled.
Try one
Two sum problem
easy
Also on MonksBit
- How to Choose the Right Data Structure for Your Algorithm Learn how to match programmatic access patterns, update frequencies, and memory budgets to the most efficient data structure. Data Structures · 8 min
- Fundamental data structure operations: insertion, deletion, searching, and traversal Master the mechanics, algorithmic complexity, and practical trade-offs of the four primary data structure operations across contiguous and linked memory layouts. Data Structures · 10 min
- Data structures and algorithms, from core concepts to system design Learn how data structures organise memory, how algorithms operate on that memory, and how to select complementary pairs to design scalable systems. Data Structures · 8 min
- How Data Structures Impact Software Performance and Scalability Discover how choosing the right data structure directly impacts software runtime, memory usage, and architectural scalability through concrete comparisons. Data Structures · 8 min
- SQL vs. NoSQL Databases: From Data Modeling to Distributed Scaling Learn the mechanical differences between relational and non-relational databases, how data models impact scaling, and how to select the right persistence layer. System Design · 10 min
- How to approach a system design problem, from scope to architecture? Master a structured framework to clarify requirements, estimate scale, design high-level architectures, and resolve deep distributed bottlenecks. System Design · 8 min
- The Importance of Time and Space Complexity in Software Engineering Master Big-O analysis of time and space to predict software performance, avoid production bottlenecks, and make sound architectural decisions. Algorithms · 8 min
- Hash tables explained, from basic buckets to high-throughput systems Master hash tables from core bucket mechanics to production applications like caching, deduplication, and index indexing in distributed systems. Data Structures · 8 min
- Hashing, from a coat check to fast lookup and caches Place keys into buckets by hand, build a chaining hash map in Python, and reuse that map as a cache that returns a stored answer instead of computing it again. Data Structures · 12 min
- Linear Search vs Binary Search in Python & PHP: Beginner to Advanced Guide How does code actually locate data under the hood? In this comprehensive, beginner-to-advanced guide, we unpack the mechanics of Linear Search and Binary Search using both Python and PHP. Whether you are walking through data structures for the first time, learning how to write robust code tests, or trying to understand how database indexes optimize real-world queries, this tutorial covers it all. Dive in to master Big-O performance trends, avoid common beginner bugs, and discover how to write se Algorithms · 33 min