CS205

Practice Exercises

Test your understanding of hash tables with these interactive exercises.

Practice Exercises
Score: 0 / 0
What is the bucket index for key "elderberry" in a HashMap with capacity 8?
hashCode("elderberry") = 683702092
Quick Reference

Formulas

bucketIndex = hashCode % capacity
loadFactor = size / capacity
threshold = capacity × 0.75
rehash when: size > threshold

Key Concepts

  • Collision: Two keys with the same bucket index
  • Chaining: Store colliding entries in a linked list
  • Load Factor: How full the table is (default 0.75)
  • Rehashing: Doubling capacity when threshold exceeded
Common Mistakes to Avoid

Forgetting modulo

Always use hashCode % capacity to get the index

Confusing size and capacity

Size = number of entries. Capacity = array length

Threshold calculation

Threshold = capacity × loadFactor (floor the result)

New indices after rehash

Use the NEW capacity in the modulo: hashCode % newCapacity