Hash Table

A Hash Table (Hash Map) is a data structure that implements an associative array abstract data type, a structure that can map keys to values using a hash function.

Core Concepts

  1. Hash Function

    • Converts keys into array indices

    • Should be deterministic

    • Should distribute values uniformly

    • Should be fast to compute

  2. Collision Resolution

    • Chaining (linked lists)

    • Open addressing (linear probing)

    • Double hashing

  3. Load Factor

    • ratio = n/k (n: entries, k: bucket count)

    • Triggers resizing when exceeded

    • Typically 0.75 in most implementations

Implementation in Go

Basic HashMap with Chaining

HashMap with Open Addressing

Time Complexity

Average Case

  • Insert: O(1)

  • Delete: O(1)

  • Search: O(1)

Worst Case (Many Collisions)

  • Insert: O(n)

  • Delete: O(n)

  • Search: O(n)

Space Complexity

  • O(n) where n is the number of key-value pairs

Common Applications

  1. Database Indexing

  2. Caching Systems

  3. Symbol Tables

  4. Counting/Frequency Tracking

  5. Deduplication

  6. Caching

  7. Session Storage

Easy

  1. 1. Two Sum

    • HashMap for complement finding

    • Time: O(n), Space: O(n)

  2. 217. Contains Duplicate

    • HashSet for tracking seen elements

    • Time: O(n), Space: O(n)

  3. 705. Design HashSet

    • Basic HashSet implementation

    • Collision handling practice

Medium

  1. 146. LRU Cache

    • HashMap + Doubly Linked List

    • O(1) operations

  2. 380. Insert Delete GetRandom O(1)

    • HashMap + Array combination

    • Constant time operations

  3. 49. Group Anagrams

    • HashMap with sorted string keys

    • Character counting

Hard

  1. 76. Minimum Window Substring

    • Sliding window with HashMap

    • Character frequency tracking

  2. 30. Substring with Concatenation of All Words

    • Multiple HashMaps

    • Sliding window technique

Best Practices

  1. Hash Function Design

    • Use prime numbers

    • Distribute keys uniformly

    • Handle different data types

    • Consider performance vs. collision trade-offs

  2. Collision Resolution

    • Choose appropriate method

    • Monitor load factor

    • Implement efficient resizing

    • Handle edge cases

  3. Memory Management

    • Implement proper cleanup

    • Handle resizing efficiently

    • Consider memory constraints

    • Clean up deleted entries

  4. Performance Optimization

    • Use efficient hash functions

    • Implement proper resizing

    • Choose appropriate initial size

    • Monitor performance metrics

Common Variations

  1. Implementation Types

    • Separate Chaining

    • Open Addressing

    • Robin Hood Hashing

    • Cuckoo Hashing

  2. Special Features

    • Thread-safe maps

    • Ordered maps

    • Concurrent maps

    • Weak reference maps

  3. Use Cases

    • Cache implementation

    • Symbol tables

    • Database indexes

    • Frequency counting

Resources

  1. Introduction to Algorithms (CLRS) - Chapter on Hash Tables

Last updated

Was this helpful?