3.3.3 · HinglishHashing

Chaining — linked lists in buckets, load factor, resizing

1,950 words9 min readRead in English

3.3.3 · Coding › Hashing


WHY chaining exist karti hai?

WHY specifically chaining? Do key strategies hain:

  • Open addressing — sab kuch array ke andar store karo, next free slot ke liye probe karo.
  • Chaining — har slot ke liye ek alag container (ek linked list) rakho.

Chaining tab choose ki jaati hai jab hum chahte hain: simple deletion, table ke crowded hone par graceful behavior, aur koi clustering ka jhanjhat nahi.


WHAT hai yeh structure?

Figure — Chaining — linked lists in buckets, load factor, resizing

HOW operations kaam karte hain (maano = stored keys ki number):

Op Kya karta hai Cost
insert(k) compute karo, list mein prepend karo
search(k) compute karo, list scan karo
delete(k) search karo, phir node ko unlink karo

Insert hai kyunki hum head pe prepend karte hain (duplicates allow ho toh check ki zaroorat nahi, ya agar duplicates reject karne hon).


Load Factor — sabse important number

Expected search cost derive karna (scratch se)


Resizing (Rehashing) — ko check mein rakhna

WHY double karo, constant add nahi? Doubling resizes ko exponentially rare banata hai.


Common Mistakes (Steel-manned)


Active Recall

Recall Reveal karne se pehle predict karo (Forecast-then-Verify)
  1. Load factor define karo aur expected search cost batao.
    ; expected .
  2. mein constant add karne ki jagah double kyun karo?
    → Geometric series amortized deti hai vs .
  3. Chained lookup ka worst case kya hai aur kab hota hai?
    , jab saari keys ek hi chain mein collide ho jaayein.
  4. Kya chaining pe break ho jaati hai?
    → Nahi, sirf open addressing karta hai; chaining gracefully degrade hoti hai.
Recall Feynman: ek 12-saal ke bachche ko explain karo

Socho ek diwar pe mailboxes hain jinhein 0–9 number diye gaye hain. Ek rule (hash) har letter ko batata hai ki woh kis box mein jaayega. Kabhi kabhi do letters ek hi box mein jaate hain — toh har box ke andar tum ek chhoti letters ki string clip karke rakhte ho (linked list). Koi letter dhundne ke liye, us ke box mein jao aur woh chhoti string flip karo. Agar boxes bahut crowded ho jaayein (average mein bahut zyada letters per box — yahi load factor hai), toh tum double boxes wali badi diwar khareedte ho aur saari letters phir se sort karte ho taaki har box halka ho. Halke boxes = fast dhundna.


Connections

  • Hash Functions ki quality decide karti hai ki uniform-hashing assumption hold hoti hai ya nahi.
  • Open Addressing — alternative (linear/quadratic probing, pe mar jaata hai).
  • Amortized Analysis inserts ko justify karta hai occasional resizes ke bawajood.
  • Dynamic Arrays — amortized append ke liye same doubling trick.
  • Pigeonhole Principle — kyun collisions unavoidable hain.
  • Linked Lists — bucket data structure khud.

Hash table ka load factor kya hota hai?
, per bucket average keys ki number ( keys, buckets).
Chained hash table mein search ki expected cost?
Simple uniform hashing ke under .
Chained hash table mein search ki worst-case cost aur yeh kab hoti hai?
, jab saari keys ek hi bucket mein hash ho jaayein (ek lambi chain).
Resize pe fixed number of slots add karne ki jagah table size double kyun karo?
Doubling total rehash work ko ek geometric series banata hai, jo amortized insert deta hai; constant growth total deta hai → per insert.
Rehashing mein kya hota hai?
Ek bada array (e.g. ) allocate karna aur har key ke liye recompute karna, unhe sab dobara insert karna.
Kya chaining pe kaam karna band kar deti hai?
Nahi — sirf open addressing pe fill up hoti hai; chaining kaam karti rehti hai aur sirf mein linearly slow hoti hai.
Chaining mein insertion (duplicate check ke bina) kyun hai?
Tum hash compute karte ho aur bucket ki list ke head pe node prepend karte ho.
Expected kaunsa assumption deta hai?
Simple Uniform Hashing: har key equally aur independently slots mein se kisi pe bhi hash ho sakti hai.
Resize trigger karne ki typical threshold?
Jab ek chosen max jaise se exceed kare.
Kisi bhi single bucket ki expected length?
.

Concept Map

since U bigger than m

forces

handled by

or by

structure is

supports

avg length equals

assumption gives

scan cost

search cost

bound a to stay fast

Hash function h maps keys to slots

Pigeonhole principle

Collisions unavoidable

Chaining

Open addressing

Linked list per bucket

Insert search delete

Load factor a equals n over m

Simple uniform hashing

Expected cost O of 1 plus a

Keep a bounded so O of 1