{"record":{"id":"e7d85cdcc08a8b89","repo":"donnemartin/interactive-coding-challenges","slug":"n-cannot-be-none","errorCode":null,"errorMessage":"n cannot be None","messagePattern":"n cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"math_probability/power_two/power_two_solution.ipynb","lineNumber":108,"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 Solution(object):\\n\",\n    \"\\n\",\n    \"    def is_power_of_two(self, n):\\n\",\n    \"        if n is None:\\n\",\n    \"            raise TypeError('n cannot be None')\\n\",\n    \"        if n <= 0:\\n\",\n    \"            return False\\n\",\n    \"        return (n & (n - 1)) == 0\"\n   ]\n  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Unit Test\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 2,\n   \"metadata\": {},\n   \"outputs\": [\n    {","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/math_probability/power_two/power_two_solution.ipynb#L90-L126","documentation":"Raised by Solution.is_power_of_two when n is None. The bit trick n & (n - 1) requires an integer; None would fail with a less clear TypeError, so the method enforces its contract explicitly.","triggerScenarios":"Calling is_power_of_two(None) — an unset variable, a None default, or a value from a lookup that missed (dict.get, next(iter, None)).","commonSituations":"Interview test harnesses hitting edge cases; passing optional config flags through without defaults.","solutions":["Pass an integer: is_power_of_two(64)","Check for None at the call site and default or skip","Fix the upstream producer returning None"],"exampleFix":"# before\nsolution.is_power_of_two(nums.get(i))  # None when i missing\n\n# after\nn = nums.get(i)\nif n is not None:\n    solution.is_power_of_two(n)","handlingStrategy":"type-guard","validationCode":"if n is None: return False\nsolution.is_power_of_two(n)","typeGuard":"def is_int(n): return isinstance(n, int)","tryCatchPattern":"try:\n    solution.is_power_of_two(n)\nexcept TypeError:\n    result = False","preventionTips":["Default numeric params to 0/sentinel, not None","Check lookups returned a value before use"],"tags":["python","input-validation","none-check","bit-manipulation"],"backgroundTag":"none-argument-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}