{
  "slug": "hash-chaining",
  "title": "Hash Table — Chaining",
  "category": "Hashing",
  "difficulty": "medium",
  "complexity": {
    "time": "O(1) average",
    "space": "O(n + m)"
  },
  "idea": "Instead of probing, each bucket holds a linked list. Colliding keys are appended to that bucket’s chain. Lookups hash to a bucket, then walk its short chain.",
  "code": [
    "bucket = k mod m",
    "append k to buckets[bucket]",
    "lookup: hash to the bucket, then walk its chain"
  ],
  "example": {
    "array": [
      15,
      11,
      27,
      8,
      12,
      21,
      6
    ]
  },
  "invariant": "Each step is the real data structure at that step — the picture is a pure function of it.",
  "stepCount": 9,
  "steps": [
    {
      "i": 0,
      "op": "lanes",
      "caption": "Insert keys into 5 buckets with h(k) = k mod 5. Collisions extend a chain.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "lanes": [
        {
          "id": "b0",
          "label": "bucket 0",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        },
        {
          "id": "b1",
          "label": "bucket 1",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        },
        {
          "id": "b2",
          "label": "bucket 2",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        },
        {
          "id": "b3",
          "label": "bucket 3",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        },
        {
          "id": "b4",
          "label": "bucket 4",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 1,
      "op": "lanes",
      "caption": "h(15) = 15 mod 5 = 0 → bucket 0 was empty; 15 starts its chain.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "lanes": [
        {
          "id": "b0",
          "label": "bucket 0",
          "array": [
            15
          ],
          "focus": [
            "insert"
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b1",
          "label": "bucket 1",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        },
        {
          "id": "b2",
          "label": "bucket 2",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        },
        {
          "id": "b3",
          "label": "bucket 3",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        },
        {
          "id": "b4",
          "label": "bucket 4",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 2,
      "op": "lanes",
      "caption": "h(11) = 11 mod 5 = 1 → bucket 1 was empty; 11 starts its chain.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "lanes": [
        {
          "id": "b0",
          "label": "bucket 0",
          "array": [
            15
          ],
          "focus": [
            null
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b1",
          "label": "bucket 1",
          "array": [
            11
          ],
          "focus": [
            "insert"
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b2",
          "label": "bucket 2",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        },
        {
          "id": "b3",
          "label": "bucket 3",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        },
        {
          "id": "b4",
          "label": "bucket 4",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 3,
      "op": "lanes",
      "caption": "h(27) = 27 mod 5 = 2 → bucket 2 was empty; 27 starts its chain.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "lanes": [
        {
          "id": "b0",
          "label": "bucket 0",
          "array": [
            15
          ],
          "focus": [
            null
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b1",
          "label": "bucket 1",
          "array": [
            11
          ],
          "focus": [
            null
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b2",
          "label": "bucket 2",
          "array": [
            27
          ],
          "focus": [
            "insert"
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b3",
          "label": "bucket 3",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        },
        {
          "id": "b4",
          "label": "bucket 4",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 4,
      "op": "lanes",
      "caption": "h(8) = 8 mod 5 = 3 → bucket 3 was empty; 8 starts its chain.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "lanes": [
        {
          "id": "b0",
          "label": "bucket 0",
          "array": [
            15
          ],
          "focus": [
            null
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b1",
          "label": "bucket 1",
          "array": [
            11
          ],
          "focus": [
            null
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b2",
          "label": "bucket 2",
          "array": [
            27
          ],
          "focus": [
            null
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b3",
          "label": "bucket 3",
          "array": [
            8
          ],
          "focus": [
            "insert"
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b4",
          "label": "bucket 4",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 5,
      "op": "lanes",
      "caption": "h(12) = 12 mod 5 = 2 → collision; append 12 to bucket 2's chain.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "lanes": [
        {
          "id": "b0",
          "label": "bucket 0",
          "array": [
            15
          ],
          "focus": [
            null
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b1",
          "label": "bucket 1",
          "array": [
            11
          ],
          "focus": [
            null
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b2",
          "label": "bucket 2",
          "array": [
            27,
            12
          ],
          "focus": [
            null,
            "insert"
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b3",
          "label": "bucket 3",
          "array": [
            8
          ],
          "focus": [
            null
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b4",
          "label": "bucket 4",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 6,
      "op": "lanes",
      "caption": "h(21) = 21 mod 5 = 1 → collision; append 21 to bucket 1's chain.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "lanes": [
        {
          "id": "b0",
          "label": "bucket 0",
          "array": [
            15
          ],
          "focus": [
            null
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b1",
          "label": "bucket 1",
          "array": [
            11,
            21
          ],
          "focus": [
            null,
            "insert"
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b2",
          "label": "bucket 2",
          "array": [
            27,
            12
          ],
          "focus": [
            null,
            null
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b3",
          "label": "bucket 3",
          "array": [
            8
          ],
          "focus": [
            null
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b4",
          "label": "bucket 4",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 7,
      "op": "lanes",
      "caption": "h(6) = 6 mod 5 = 1 → collision; append 6 to bucket 1's chain.",
      "note": null,
      "codeLine": 2,
      "stats": null,
      "lanes": [
        {
          "id": "b0",
          "label": "bucket 0",
          "array": [
            15
          ],
          "focus": [
            null
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b1",
          "label": "bucket 1",
          "array": [
            11,
            21,
            6
          ],
          "focus": [
            null,
            null,
            "insert"
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b2",
          "label": "bucket 2",
          "array": [
            27,
            12
          ],
          "focus": [
            null,
            null
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b3",
          "label": "bucket 3",
          "array": [
            8
          ],
          "focus": [
            null
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b4",
          "label": "bucket 4",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 8,
      "op": "lanes",
      "caption": "All keys placed. Each bucket holds a short chain to walk on lookup.",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "lanes": [
        {
          "id": "b0",
          "label": "bucket 0",
          "array": [
            15
          ],
          "focus": [
            null
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b1",
          "label": "bucket 1",
          "array": [
            11,
            21,
            6
          ],
          "focus": [
            null,
            null,
            null
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b2",
          "label": "bucket 2",
          "array": [
            27,
            12
          ],
          "focus": [
            null,
            null
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b3",
          "label": "bucket 3",
          "array": [
            8
          ],
          "focus": [
            null
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "b4",
          "label": "bucket 4",
          "array": [],
          "focus": [],
          "markers": [],
          "regions": []
        }
      ]
    }
  ]
}