TGViewer
Coding Interview Preparation Coding Interview Preparation @coding_interview_preparation · 5.9K subscribers
Post #1362 370
🧠 EDUCATIONAL CS #2 - Why Hash Tables Are Basically Magic

You use hash tables constantly (Python dicts, JS objects, Java HashMaps) - but do you know why they're O(1)?

Here's the core idea:

1️⃣ You have a hash function that takes a key and turns it into a number (an index).
2️⃣ That index points directly to a slot ("bucket") in an array.
3️⃣ To look up a value, you hash the key again, jump straight to that slot - no searching required.


key "apple" → hash("apple") → index 7 → array[7] = value


That's why lookup, insert, and delete are all O(1) on average.

Why "on average" and not always? Because two different keys can hash to the same index - a collision. When that happens, most implementations chain multiple entries in the same bucket (a small linked list) or probe for the next open slot.

If your hash function is bad and everything collides into one bucket, your "O(1)" hash table quietly degrades into an O(n) linked list. This is exactly why interviewers sometimes ask: "what happens if all your keys hash to the same value?"

Now you know the answer. 😉

What's a bug you've hit because of hash collisions or bad hashing? 👇
More from @coding_interview_preparation
  1. Oct 8, 2026If you're prepping for system design interviews, this repo is gold It contains a curated,…
  2. Oct 6, 2026document post
  3. Oct 4, 2026💼 Why Your Resume Gets Rejected Before a Human Reads It You may have good skills and proj…
  4. Oct 2, 2026🧠 Coding Myths You Should Stop Believing There's a lot of advice online about learning to…
  5. Oct 1, 2026Most Asked Topics in AI Engineer Interviews Based on 2026 candidate reports
  6. Sep 30, 2026💼 What Companies Actually Look For in a Fresher Think companies only care about your CGPA…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →