{
    "slug": "skip_list",
    "term": "Skip Lists",
    "category": "data_structures",
    "difficulty": "advanced",
    "short": "A probabilistic data structure providing O(log n) search, insert, and delete — used internally in Redis sorted sets for ZADD/ZRANGE operations.",
    "long": "A skip list is a layered linked list: the bottom layer has all elements in sorted order; each higher layer contains a random subset of the elements below, acting as express lanes. Search: start at the top layer, move right until the next element is too large, then drop down — O(log n) expected. Insert: coin flips determine which layers the new element appears in. Redis uses skip lists for sorted sets (ZADD/ZRANGE). Advantages over balanced BSTs: simpler implementation, easier lock-free concurrent modification.",
    "aliases": [
        "skip list",
        "probabilistic data structure",
        "Redis sorted set"
    ],
    "tags": [
        "data-structures",
        "algorithms",
        "probabilistic"
    ],
    "misconception": "Skip lists are always inferior to balanced BSTs because they are probabilistic — they have the same expected O(log n) complexity and are often faster in practice due to better cache performance and simpler concurrent implementations.",
    "why_it_matters": "Redis sorted set operations (ZADD, ZRANGEBYSCORE) are O(log n) because of skip lists — understanding this explains why bulk inserts should be pipelined and why ZRANGEBYSCORE is O(log n + M).",
    "common_mistakes": [
        "Fixed levels instead of probabilistic promotion — breaks expected O(log n)",
        "Maximum level too low — degrades to O(n)",
        "Using skip list where a hash map suffices — add ordering only when needed",
        "Concurrent modifications without proper synchronisation"
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "b_tree_structure",
        "binary_tree",
        "hash_table",
        "adjacency_matrix_list"
    ],
    "prerequisites": [
        "linked_list",
        "big_o_notation",
        "hash_table"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/Skip_list"
    ],
    "bad_code": "// Assuming zadd is O(1):\nfor ($i = 0; $i < 1000000; $i++) {\n    $redis->zadd('leaderboard', $score, $userId);\n    // 1M * O(log n) = O(n log n) total — expected but surprising\n}",
    "good_code": "// Pipeline for bulk inserts:\n$pipeline = $redis->pipeline();\nforeach ($scores as $userId => $score) {\n    $pipeline->zadd('leaderboard', $score, $userId);\n}\n$pipeline->execute(); // Single round trip for all insertions",
    "quick_fix": "Use Redis sorted sets (which implement skip lists internally) for ordered data with O(log n) rank queries — no need to implement skip lists in PHP directly",
    "severity": "info",
    "effort": "high",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/skip_list",
        "html_url": "https://codeclaritylab.com/glossary/skip_list",
        "json_url": "https://codeclaritylab.com/glossary/skip_list.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Skip Lists](https://codeclaritylab.com/glossary/skip_list) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/skip_list"
            }
        }
    }
}