{"record":{"id":"7fb711d37b073a5b","repo":"donnemartin/interactive-coding-challenges","slug":"array-cannot-be-none-or-empty","errorCode":null,"errorMessage":"array cannot be None or empty","messagePattern":"array cannot be None or empty","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sorting_searching/new_int/new_int_solution.ipynb","lineNumber":110,"sourceCode":"   \"metadata\": {},\n   \"source\": [\n    \"## Code\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 1,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"from bitstring import BitArray  # Run pip install bitstring\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"class Bits(object):\\n\",\n    \"\\n\",\n    \"    def new_int(self, array, max_size):\\n\",\n    \"        if not array:\\n\",\n    \"            raise TypeError('array cannot be None or empty')\\n\",\n    \"        bit_vector = BitArray(max_size)\\n\",\n    \"        for item in array:\\n\",\n    \"            bit_vector[item] = True\\n\",\n    \"        for index, item in enumerate(bit_vector):\\n\",\n    \"            if not item:\\n\",\n    \"                return index\\n\",\n    \"        return None\"\n   ]\n  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Unit Test\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/sorting_searching/new_int/new_int_solution.ipynb#L92-L128","documentation":"Bits.new_int raises TypeError('array cannot be None or empty') because the algorithm maps input integers into a BitArray bit vector and then scans for the first unset bit; with no input there is nothing to map and the result would be meaningless. The 'not array' check rejects both None and empty lists.","triggerScenarios":"Calling Bits().new_int(None, max_size) or Bits().new_int([], max_size). Also triggered when max_size is omitted/misused so the array argument ends up empty.","commonSituations":"Feeding an empty result set from a previous computation into the missing-integer finder; optional function parameters defaulting to None; test harnesses checking boundary conditions.","solutions":["Verify the array is populated before calling new_int; skip the call for empty inputs","Check the upstream producer of the array (loop/file read) to see why it yielded no items","If an empty array legitimately means 'answer is 0', handle that case in the caller instead of calling new_int"],"exampleFix":"// before\nindex = Bits().new_int(array, max_size)  # array may be []\n\n// after\nindex = Bits().new_int(array, max_size) if array else 0","handlingStrategy":"validation","validationCode":"if not array:\n    # nothing to map; first free index is 0 by convention\n    return 0\nindex = Bits().new_int(array, max_size)","typeGuard":"def has_items(x):\n    return isinstance(x, list) and len(x) > 0","tryCatchPattern":"try:\n    index = bits.new_int(array, max_size)\nexcept TypeError:\n    index = 0  # empty/None input treated as answer 0","preventionTips":["Check 'if not array' before calling","Ensure loops producing array actually append items","Distinguish None (error) from [] (empty) in data producers"],"tags":["python","input-validation","typeerror","bit-manipulation"],"backgroundTag":"empty-input-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}