{"record":{"id":"2e219599b38081a0","repo":"donnemartin/interactive-coding-challenges","slug":"k-cannot-be-none","errorCode":null,"errorMessage":"k cannot be None","messagePattern":"k cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"online_judges/longest_substr_k_distinct/longest_substr_solution.ipynb","lineNumber":104,"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 longest_substr(self, string, k):\\n\",\n    \"        if string is None:\\n\",\n    \"            raise TypeError('string cannot be None')\\n\",\n    \"        if k is None:\\n\",\n    \"            raise TypeError('k cannot be None')\\n\",\n    \"        low_index = 0\\n\",\n    \"        max_length = 0\\n\",\n    \"        chars_to_index_map = {}\\n\",\n    \"        for index, char in enumerate(string):\\n\",\n    \"            chars_to_index_map[char] = index\\n\",\n    \"            if len(chars_to_index_map) > k:\\n\",\n    \"                low_index = min(chars_to_index_map.values())\\n\",\n    \"                del chars_to_index_map[string[low_index]]\\n\",\n    \"                low_index += 1\\n\",\n    \"            max_length = max(max_length, index - low_index + 1)\\n\",\n    \"        return max_length\"\n   ]\n  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Unit Test\"","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/online_judges/longest_substr_k_distinct/longest_substr_solution.ipynb#L86-L122","documentation":"Raised by Solution.longest_substr when k is None. k is compared as len(chars_to_index_map) > k; None would break that comparison, so the method requires an integer k (the max number of distinct characters) explicitly.","triggerScenarios":"Calling longest_substr('abc', None) — usually a k read from argv/config that was never set, or an optional parameter defaulting to None.","commonSituations":"CLI parsing where k = args.k with no default; config-driven workloads with a missing key; tests asserting the guard.","solutions":["Pass an integer k, e.g. longest_substr('eceba', 2)","Default at the call site: k = 2 if k is None else k","Validate config/argv before the call and fail with a clear message"],"exampleFix":"# before\nsolution.longest_substr(s, config.get('k'))\n\n# after\nk = config.get('k')\nif k is None:\n    raise ValueError('k is required')\nsolution.longest_substr(s, int(k))","handlingStrategy":"validation","validationCode":"if k is None: raise ValueError('k is required')\nsolution.longest_substr(string, int(k))","typeGuard":"def is_int(x): return isinstance(x, int)","tryCatchPattern":"try:\n    n = solution.longest_substr(s, k)\nexcept TypeError:\n    n = solution.longest_substr(s, DEFAULT_K)","preventionTips":["Set explicit defaults for k in CLI/config parsing","Cast and validate numeric params once at entry"],"tags":["python","input-validation","none-check","sliding-window"],"backgroundTag":"none-argument-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}