{"record":{"id":"23de27303bd729c2","repo":"donnemartin/interactive-coding-challenges","slug":"invalid-arg-x1-or-x2-out-of-bounds","errorCode":null,"errorMessage":"Invalid arg: x1 or x2 out of bounds","messagePattern":"Invalid arg: x1 or x2 out of bounds","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/draw_line/draw_line_solution.ipynb","lineNumber":166,"sourceCode":"    \"## Code\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 1,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"class BitsScreen(object):\\n\",\n    \"\\n\",\n    \"    def draw_line(self, screen, width, x1, x2):\\n\",\n    \"        if None in (screen, width, x1, x2):\\n\",\n    \"            raise TypeError('Invalid argument: None')\\n\",\n    \"        if not screen or not width:\\n\",\n    \"            raise ValueError('Invalid arg: Empty screen or width')\\n\",\n    \"        MAX_BIT_VALUE = len(screen) * 8\\n\",\n    \"        if x1 < 0 or x2 < 0 or x1 >= MAX_BIT_VALUE or x2 >= MAX_BIT_VALUE:\\n\",\n    \"            raise ValueError('Invalid arg: x1 or x2 out of bounds')\\n\",\n    \"        start_bit = x1 % 8\\n\",\n    \"        end_bit = x2 % 8\\n\",\n    \"        first_full_byte = x1 // 8\\n\",\n    \"        if start_bit != 0:\\n\",\n    \"            first_full_byte += 1\\n\",\n    \"        last_full_byte = x2 // 8\\n\",\n    \"        if end_bit != (8 - 1):\\n\",\n    \"            last_full_byte -= 1\\n\",\n    \"        for byte in range(first_full_byte, last_full_byte + 1):\\n\",\n    \"            screen[byte] = int('11111111', base=2)\\n\",\n    \"        start_byte = x1 // 8\\n\",\n    \"        end_byte = x2 // 8\\n\",\n    \"        if start_byte == end_byte:\\n\",\n    \"            left_mask = (1 << (8 - start_bit)) - 1\\n\",\n    \"            right_mask = ~((1 << (8 - end_bit - 1)) - 1)\\n\",\n    \"            mask = left_mask & right_mask\\n\",\n    \"            screen[start_byte] |= mask\\n\",\n    \"        else:\\n\",","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/bit_manipulation/draw_line/draw_line_solution.ipynb#L148-L184","documentation":"BitsScreen.draw_line raises ValueError when x1 or x2 falls outside the screen: negative, or >= len(screen) * 8 (the total number of bits available). This bounds check runs after the None and empty-input checks.","triggerScenarios":"draw_line(screen, 8, -1, 4), or draw_line(screen, 8, 0, 8) on a 1-byte screen where MAX_BIT_VALUE is 8 and x2 == 8; also passing pixel coordinates where the code expects bit coordinates.","commonSituations":"Confusing width with total bit capacity, off-by-one at the right edge (x2 == MAX_BIT_VALUE instead of MAX_BIT_VALUE - 1), or reusing coordinates from a larger screen on a smaller buffer.","solutions":["Clamp coordinates: x = max(0, min(x, len(screen) * 8 - 1))","Fix off-by-one: use x2 = width - 1, not width","Verify coordinate space (bits, not bytes/pixels) matches the API expectation"],"exampleFix":"# before\nbits.draw_line(screen, 16, 0, 16)  # 2-byte screen, 16 is out of bounds\n\n# after\nbits.draw_line(screen, 16, 0, 15)","handlingStrategy":"validation","validationCode":"max_bit = len(screen) * 8\nx1, x2 = max(0, x1), max(0, x2)\nif x1 >= max_bit or x2 >= max_bit:\n    raise ValueError(f'coordinates must be < {max_bit}')\nbits_screen.draw_line(screen, width, x1, x2)","typeGuard":"def in_bounds(screen, x) -> bool:\n    return 0 <= x < len(screen) * 8","tryCatchPattern":"try:\n    bits_screen.draw_line(screen, width, x1, x2)\nexcept ValueError as e:\n    if 'out of bounds' in str(e):\n        x2 = min(x2, len(screen) * 8 - 1)\n        bits_screen.draw_line(screen, width, x1, x2)\n    else:\n        raise","preventionTips":["Remember x1/x2 are bit coordinates capped at len(screen)*8 - 1","End ranges at width-1, not width","Centralize coordinate clamping in one helper before any draw call"],"tags":["python","out-of-bounds","value-error","graphics","off-by-one"],"backgroundTag":"index-out-of-bounds","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}