DSA
    intermediate

    Hash Map: Collision Handling

    Software Engineer path · lesson 6 of 99
    time O(1)
    space O(n)

    Syntax: put word n · get word · remove word. Numbers from -99 to 99.

    Try:
    Buckets m

    Fewer buckets, longer chains: watch key comparisons.

    [0][1][2][3][4]
    Bucket being scannedComparing keyMatchWrittenEmpty

    5 empty buckets. A key's bucket is the sum of its character codes mod 5.

    Code (running line highlighted)
    1. 1h = (sum of character codes of key) % m
    2. 2put(k, v): scan bucket h; update k if found, else append (k, v)
    3. 3get(k): scan bucket h for k
    4. 4remove(k): scan bucket h; unlink k if found
    5. 5load factor α = entries / m — long chains mean slow lookups
    Hashes
    0 / 7
    Key comparisons
    0 / 4
    Invariant: Every key lives in bucket hash(key) % 5. Load factor 0.00.

    Tap any element in the diagram to inspect its state at this step.

    1 / 20

    Keys: ← → step, Space play/pause. Predict mode pauses before key steps and asks what happens next.

    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
    function twoSum(nums, target) {
      // Your code here
    }

    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