{"record":{"id":"0a7eb3461fbb14d7","repo":"donnemartin/interactive-coding-challenges","slug":"invalid-event-type","errorCode":null,"errorMessage":"Invalid event type","messagePattern":"Invalid event type","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"online_judges/busiest_period/busiest_period_solution.ipynb","lineNumber":181,"sourceCode":"   \"source\": [\n    \"class Solution(object):\\n\",\n    \"\\n\",\n    \"    def find_busiest_period(self, data):\\n\",\n    \"        if data is None:\\n\",\n    \"            raise TypeError('data cannot be None')\\n\",\n    \"        if not data:\\n\",\n    \"            return None\\n\",\n    \"        data.sort()\\n\",\n    \"        max_period = Period(0, 0)\\n\",\n    \"        max_people = 0\\n\",\n    \"        curr_people = 0\\n\",\n    \"        for index, interval in enumerate(data):\\n\",\n    \"            if interval.event_type == EventType.ENTER:\\n\",\n    \"                curr_people += interval.num_people\\n\",\n    \"            elif interval.event_type == EventType.EXIT:\\n\",\n    \"                curr_people -= interval.num_people\\n\",\n    \"            else:\\n\",\n    \"                raise ValueError('Invalid event type')\\n\",\n    \"            if (index < len(data) - 1 and \\n\",\n    \"                    data[index].timestamp == data[index + 1].timestamp):\\n\",\n    \"                continue\\n\",\n    \"            if curr_people > max_people:\\n\",\n    \"                max_people = curr_people\\n\",\n    \"                max_period.start = data[index].timestamp\\n\",\n    \"                if index < len(data) - 1:\\n\",\n    \"                    max_period.end = data[index + 1].timestamp\\n\",\n    \"                else:\\n\",\n    \"                    max_period.end = data[index].timestamp\\n\",\n    \"        return max_period\"\n   ]\n  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Unit Test\"","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/online_judges/busiest_period/busiest_period_solution.ipynb#L163-L199","documentation":"Raised inside Solution.find_busiest_period while iterating the sorted event list: an interval's event_type is neither EventType.ENTER nor EventType.EXIT. The visitor-count arithmetic depends on the event direction, so unknown types are rejected mid-loop with ValueError.","triggerScenarios":"Calling find_busiest_period with an Interval whose event_type is e.g. 2, 'enter', or a typo'd enum member — anything outside {EventType.ENTER, EventType.EXIT}.","commonSituations":"Constructing test data with raw ints/strings instead of enum members; enum refactors that renamed or added members; deserializing events from JSON where the type field wasn't mapped to the enum.","solutions":["Map event types to the enum when building data: EventType.ENTER for arrivals, EventType.EXIT for departures","Validate event_type in your Interval factory/deserializer before calling find_busiest_period","Check for enum renames if code worked before a version change"],"exampleFix":"# before\nInterval(1, 10, 1)  # raw int not in EventType -> ValueError\n\n# after\nInterval(1, 10, EventType.ENTER)","handlingStrategy":"type-guard","validationCode":"valid = {EventType.ENTER, EventType.EXIT}\nfor iv in data:\n    if iv.event_type not in valid: raise ValueError('bad event_type')\nsolution.find_busiest_period(data)","typeGuard":"def is_valid_event(t): return t in (EventType.ENTER, EventType.EXIT)","tryCatchPattern":"try:\n    solution.find_busiest_period(data)\nexcept ValueError as e:\n    # drop or re-map invalid events, then retry\n    data = [iv for iv in data if is_valid_event(iv.event_type)]\n    solution.find_busiest_period(data)","preventionTips":["Always construct Intervals with enum members, not raw ints/strings","Centralize event deserialization through a validator"],"tags":["python","enum","input-validation","intervals","event-processing"],"backgroundTag":"invalid-enum-value","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}