{"record":{"id":"2f9bc4efb46158fa","repo":"donnemartin/interactive-coding-challenges","slug":"k-must-be-of-type-int","errorCode":null,"errorMessage":"k must be of type int","messagePattern":"k must be of type int","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"recursion_dynamic/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 not isinstance(string, str):\\n\",\n    \"            raise TypeError('string must be of type str')\\n\",\n    \"        if not isinstance(k, int):\\n\",\n    \"            raise TypeError('k must be of type int')\\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/recursion_dynamic/longest_substr_k_distinct/longest_substr_solution.ipynb#L86-L122","documentation":"Raised by Solution.longest_substr when k is not an int (including None). It is the second guard, after the string check, because the algorithm compares len(chars_to_index_map) > k numerically; a non-int k would make that comparison meaningless or raise elsewhere.","triggerScenarios":"Calling longest_substr('abc', None), longest_substr('abc', '2'), or longest_substr('abc', 2.0). Note bool is a subclass of int and would pass this check.","commonSituations":"Taking k from CLI args or query strings where it arrives as str; k read from JSON config as float; forgotten default leaving k=None.","solutions":["Convert at the boundary: k = int(k) for str/float inputs","Provide a concrete default: def longest_substr(string, k=1) at your own wrapper","Validate k is int (and >= 0) before calling"],"exampleFix":"// before\nsol.longest_substr(s, k=request.args.get('k'))\n// after\nsol.longest_substr(s, k=int(request.args.get('k', 2)))","handlingStrategy":"validation","validationCode":"k = int(k) if k is not None else 1\nsol.longest_substr(string, k)","typeGuard":"def is_int(x):\n    return isinstance(x, int) and not isinstance(x, bool)","tryCatchPattern":"try:\n    sol.longest_substr(string, k)\nexcept TypeError:\n    k = int(k)\n    n = sol.longest_substr(string, k)","preventionTips":["Cast CLI/query params to int immediately","Set explicit defaults for k"],"tags":["python","type-check","integer","sliding-window","input-validation"],"backgroundTag":"wrong-argument-type","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}