{"record":{"id":"1e193056b9a72f33","repo":"donnemartin/interactive-coding-challenges","slug":"array-or-val-cannot-be-none","errorCode":null,"errorMessage":"array or val cannot be None","messagePattern":"array or val cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sorting_searching/rotated_array_search/rotated_array_search_solution.ipynb","lineNumber":140,"sourceCode":"  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Code\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 1,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"class Array(object):\\n\",\n    \"\\n\",\n    \"    def search_sorted_array(self, array, val):\\n\",\n    \"        if array is None or val is None:\\n\",\n    \"            raise TypeError('array or val cannot be None')\\n\",\n    \"        if not array:\\n\",\n    \"            return None\\n\",\n    \"        return self._search_sorted_array(array, val, start=0, end=len(array) - 1)\\n\",\n    \"\\n\",\n    \"    def _search_sorted_array(self, array, val, start, end):\\n\",\n    \"        if end < start:\\n\",\n    \"            return None\\n\",\n    \"        mid = (start + end) // 2\\n\",\n    \"        if array[mid] == val:\\n\",\n    \"            return mid\\n\",\n    \"        # Left side is sorted\\n\",\n    \"        if array[start] < array[mid]:\\n\",\n    \"            if array[start] <= val < array[mid]:\\n\",\n    \"                return self._search_sorted_array(array, val, start, mid - 1)\\n\",\n    \"            else:\\n\",\n    \"                return self._search_sorted_array(array, val, mid + 1, end)\\n\",\n    \"        # Right side is sorted\\n\",\n    \"        elif array[start] > array[mid]:\\n\",","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/sorting_searching/rotated_array_search/rotated_array_search_solution.ipynb#L122-L158","documentation":"Array.search_sorted_array raises TypeError('array or val cannot be None') if either the array to search or the target value is None. The binary-search-style recursion needs both a valid array and a comparable target; None for either is a caller bug, while an empty array is legal and returns None.","triggerScenarios":"Calling search_sorted_array(None, 5) or search_sorted_array([1,2,3], None); commonly the val comes from a dict .get() or user input that returned None.","commonSituations":"Lookups keyed by optional identifiers (dict.get returning None, missing query params); test cases for boundary conditions; passing an uninitialized search target.","solutions":["Check both arguments before calling: skip or default when array or val is None","Fix the source of the None val (e.g. dict.get(key, default) or validate user input)","For optional search targets, only invoke the search when val is known"],"exampleFix":"// before\nresult = Array().search_sorted_array(arr, val)  # val may be None\n\n// after\nresult = Array().search_sorted_array(arr, val) if val is not None else None","handlingStrategy":"validation","validationCode":"if array is None or val is None:\n    return None  # or raise your own clearer error\nreturn Array().search_sorted_array(array, val)","typeGuard":"def can_search(arr, v):\n    return isinstance(arr, list) and arr and v is not None","tryCatchPattern":"try:\n    idx = a.search_sorted_array(array, val)\nexcept TypeError as e:\n    if 'cannot be None' in str(e):\n        idx = None\n    else:\n        raise","preventionTips":["Validate dict.get()/user-input results for the val before searching","Keep array and val provenance explicit (no optional-None flow)","Return early when either input is missing"],"tags":["python","input-validation","typeerror","binary-search"],"backgroundTag":"none-input-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}