Learn C
#include "systems/hash_table.h"

Capstone: Hash Table in C

#concept

A hash table with separate chaining distills the course into one project: string keys hashed (djb2: hash = hash * 33 + c) modulo bucket count, collisions chained through linked-list nodes, with strdup'd keys, load-factor-triggered rehashing, and full create/insert/find/delete/destroy lifecycle.

It exercises strings, pointers, structs, dynamic memory, ownership (who frees keys?), and function pointers (custom hash/compare) simultaneously — the data structure behind interpreters' symbol tables and countless caches, built here from raw C with no library help.

#how it works
1

Hash to a bucket

djb2 loops hash = hash * 33 + (unsigned char)c over key bytes — simple, fast, well-distributed for strings.

2

Chain collisions

Each bucket heads a linked list of entries — collisions append, lookups walk and strcmp.

3

Own the keys

strdup keys on insert, free on delete/destroy — ownership documented once, honored everywhere.

4

Rehash on load

Past ~0.75 load factor, double buckets and reinsert all — amortized O(1) preserved as data grows.

#example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
unsigned long hash_str(const char *s) {
unsigned long h = 5381;
while (*s) h = h * 33 + (unsigned char)*s++;
return h;
}
 
int main(void) {
printf("%lu\n", hash_str("a") % 8);
printf("%lu\n", hash_str("b") % 8);
return 0;
}
Output
6
7
1djb2 starts at 5381 and folds each byte — deterministic, well-spread string hashing in four lines.
2"a" hashes to bucket 6 of 8, "b" to bucket 7 — neighboring keys landing apart, as desired.
3The hash core verified; chaining, ownership, and rehash wrap it into the full table.
#complexity
time O(1) average ops
space O(n)

Degrades toward O(n) buckets under pathological hashing or overload.

#use cases
Symbol tables and caches with average O(1) lookup
Counting and interning strings with owned-key discipline
Rehashing on load factor to preserve amortized performance
Custom hash/compare injection for exotic key types

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.