DSA
    intermediate

    Hash Map: Collision Handling

    Hash Map (Separate Chaining)

    Visualize how a hash map stores key-value pairs and resolves collisions by chaining multiple entries in the same bucket.

    ⏱ O(1)
    💾 O(n)
    Ready
    00
    empty
    10
    empty
    20
    empty
    30
    empty
    40
    empty
    Press ▶ to auto-demo, or enter a key/value and click Put.
    50%
    Live Variables
    bucketCount5
    totalItems0
    collisions0
    lastBucket—
    demoStep0/8

    put(key, value) with chaining

    Sequence

    Order what a chained hash map does on insert.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    1. 1Drop a step here
    2. 2Drop a step here
    3. 3Drop a step here
    4. 4Drop a step here
    5. 5Drop a step here
    5 pieces left to place

    Test your knowledge

    20 questions
    Question 1 of 20
    Score: 0

    What is the worst-case time complexity of searching in a Hash Map with Chaining?

    Write the code

    Runs your solution

    Two Sum

    Given an array of integers `nums` and a `target`, return indices of the two numbers such that they add up to `target`.

    Example Cases

    Input:[[2,7,11,15], 9]
    Output:[0,1]
    Input:[[3,2,4], 6]
    Output:[1,2]
    Input:[[3,3], 6]
    Output:[0,1]
    javascript

    The short version

    Think of a library where every book has a specific shelf determined by a formula based on its title. You don't browse; you calculate the exact location and go straight there.

    In more depth

    Constant Time Magic

    A Hash Map (or Hash Table) allows for O(1) average time complexity for Insert, Delete, and Lookup. It works by using a Hash Function to map a key (like a string) to an index in an array.

    Collisions:

    When two keys map to the same index.

    Chaining: Store a list of items at that index.
    Open Addressing: Find the next open slot.

    Hash Maps are the single most useful data structure in interviews for trading space for time.

    When to reach for this

    • Count frequency of elements
    • Check existence in O(1)
    • Map a key to a value (Caching)

    Interview Questions

    Amazon
    Microsoft
    Google
    Amazon
    Google
    Facebook
    Google
    Facebook
    Amazon
    Google
    Microsoft
    Facebook
    Uber
    Bloomberg

    Love this visualization?

    Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.

    100% free • No credit card required