{"record":{"id":"e4ca95e931e633c4","repo":"TheAlgorithms/Python","slug":"empty-number-list-not-allowed","errorCode":null,"errorMessage":"empty number list not allowed","messagePattern":"empty number list not allowed","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"data_structures/arrays/sparse_table.py","lineNumber":32,"sourceCode":"from math import log2\n\n\ndef build_sparse_table(number_list: list[int]) -> list[list[int]]:\n    \"\"\"\n    Precompute range minimum queries with power of two length and store the precomputed\n    values in a table.\n\n    >>> build_sparse_table([8, 1, 0, 3, 4, 9, 3])\n    [[8, 1, 0, 3, 4, 9, 3], [1, 0, 0, 3, 4, 3, 0], [0, 0, 0, 3, 0, 0, 0]]\n    >>> build_sparse_table([3, 1, 9])\n    [[3, 1, 9], [1, 1, 0]]\n    >>> build_sparse_table([])\n    Traceback (most recent call last):\n    ...\n    ValueError: empty number list not allowed\n    \"\"\"\n    if not number_list:\n        raise ValueError(\"empty number list not allowed\")\n\n    length = len(number_list)\n    # Initialise sparse_table -- sparse_table[j][i] represents the minimum value of the\n    # subset of length (2 ** j) of number_list, starting from index i.\n\n    # smallest power of 2 subset length that fully covers number_list\n    row = int(log2(length)) + 1\n    sparse_table = [[0 for i in range(length)] for j in range(row)]\n\n    # minimum of subset of length 1 is that value itself\n    for i, value in enumerate(number_list):\n        sparse_table[0][i] = value\n    j = 1\n\n    # compute the minimum value for all intervals with size (2 ** j)\n    while (1 << j) <= length:\n        i = 0\n        # while subset starting from i still have at least (2 ** j) elements","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/arrays/sparse_table.py#L14-L50","documentation":"Raised by build_sparse_table() in data_structures/arrays/sparse_table.py when number_list is empty. The sparse table answers range-minimum queries via powers of two (int(log2(length)) rows); with length 0, log2(0) is undefined, so construction is refused up front.","triggerScenarios":"Calling build_sparse_table([]), or building the table from data that a filter reduced to nothing.","commonSituations":"RMQ over sliding windows/buckets where a bucket can be empty, empty input files or query results feeding the table builder, or initializing tables for arrays whose size is determined at runtime and can be zero.","solutions":["Skip table construction for empty input: if data: table = build_sparse_table(data).","Validate at the data-ingestion boundary so empty datasets fail with a domain-specific error.","For bucketed RMQ, either skip empty buckets or give them a neutral sentinel before building."],"exampleFix":"# before\ntable = build_sparse_table(values)  # values == []\n\n# after\ntable = build_sparse_table(values) if values else None","handlingStrategy":"validation","validationCode":"if not values:\n    raise ValueError('cannot build sparse table for empty data')\ntable = build_sparse_table(values)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Skip RMQ table construction for empty buckets/windows","Reject empty datasets at ingestion"],"tags":["value-validation","sparse-table","rmq","empty-input"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}