{"record":{"id":"271506ae6c6d2424","repo":"donnemartin/interactive-coding-challenges","slug":"nums-cannot-be-empty","errorCode":null,"errorMessage":"nums cannot be empty","messagePattern":"nums cannot be empty","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"arrays_strings/two_sum/two_sum_solution.ipynb","lineNumber":168,"sourceCode":"   \"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 Solution(object):\\n\",\n    \"\\n\",\n    \"    def two_sum(self, nums, target):\\n\",\n    \"        if nums is None or target is None:\\n\",\n    \"            raise TypeError('nums or target cannot be None')\\n\",\n    \"        if not nums:\\n\",\n    \"            raise ValueError('nums cannot be empty')\\n\",\n    \"        cache = {}\\n\",\n    \"        for index, num in enumerate(nums):\\n\",\n    \"            cache_target = target - num\\n\",\n    \"            if num in cache:\\n\",\n    \"                return [cache[num], index]\\n\",\n    \"            else:\\n\",\n    \"                cache[cache_target] = index\\n\",\n    \"        return None\"\n   ]\n  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Unit Test\"\n   ]\n  },\n  {","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/arrays_strings/two_sum/two_sum_solution.ipynb#L150-L186","documentation":"Raised by Solution.two_sum when nums is an empty list. With no elements there can be no pair summing to target, and the implementation distinguishes 'no input' (ValueError) from 'no solution' (returns None). It fires after the None check, so nums must be non-None AND non-empty.","triggerScenarios":"Calling two_sum([], 9); filtering a list before passing it and ending up with zero elements; early-morning data loads producing empty inputs.","commonSituations":"Chained filters/comprehensions that can legitimately yield []; processing batches where some batches are empty; confusing 'empty input' with 'no answer found'.","solutions":["Skip the call when the list is empty: if nums: result = two_sum(nums, target)","Treat empty input as an explicit domain case in your handler (400 or empty result) rather than an exception","Validate batch size before entering the two-sum step"],"exampleFix":"# before\nresult = Solution().two_sum(nums, target)  # nums may be []\n\n# after\nresult = Solution().two_sum(nums, target) if nums else None","handlingStrategy":"validation","validationCode":"if nums:\n    Solution().two_sum(nums, target)","typeGuard":"def is_non_empty_list(nums) -> bool:\n    return isinstance(nums, list) and len(nums) > 0","tryCatchPattern":"try:\n    Solution().two_sum(nums, target)\nexcept ValueError as e:\n    if 'empty' in str(e):\n        return None\n    raise","preventionTips":["Guard empty batches before processing","Treat empty input as a domain case, not an exception"],"tags":["two-sum","empty-list","valueerror","validation"],"backgroundTag":"empty-collection-argument","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}