donnemartin/interactive-coding-challenges · error · ValueError
Invalid event type
Error message
Invalid event type
What it means
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.
Source
Thrown at online_judges/busiest_period/busiest_period_solution.ipynb:181
"source": [
"class Solution(object):\n",
"\n",
" def find_busiest_period(self, data):\n",
" if data is None:\n",
" raise TypeError('data cannot be None')\n",
" if not data:\n",
" return None\n",
" data.sort()\n",
" max_period = Period(0, 0)\n",
" max_people = 0\n",
" curr_people = 0\n",
" for index, interval in enumerate(data):\n",
" if interval.event_type == EventType.ENTER:\n",
" curr_people += interval.num_people\n",
" elif interval.event_type == EventType.EXIT:\n",
" curr_people -= interval.num_people\n",
" else:\n",
" raise ValueError('Invalid event type')\n",
" if (index < len(data) - 1 and \n",
" data[index].timestamp == data[index + 1].timestamp):\n",
" continue\n",
" if curr_people > max_people:\n",
" max_people = curr_people\n",
" max_period.start = data[index].timestamp\n",
" if index < len(data) - 1:\n",
" max_period.end = data[index + 1].timestamp\n",
" else:\n",
" max_period.end = data[index].timestamp\n",
" return max_period"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"View on GitHub (pinned to 358f2cc604)
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
Example fix
# before Interval(1, 10, 1) # raw int not in EventType -> ValueError # after Interval(1, 10, EventType.ENTER)
Defensive patterns
Strategy: type-guard
Validate before calling
valid = {EventType.ENTER, EventType.EXIT}
for iv in data:
if iv.event_type not in valid: raise ValueError('bad event_type')
solution.find_busiest_period(data) Type guard
def is_valid_event(t): return t in (EventType.ENTER, EventType.EXIT)
Try / catch
try:
solution.find_busiest_period(data)
except ValueError as e:
# drop or re-map invalid events, then retry
data = [iv for iv in data if is_valid_event(iv.event_type)]
solution.find_busiest_period(data) Prevention
- Always construct Intervals with enum members, not raw ints/strings
- Centralize event deserialization through a validator
When it happens
Trigger: 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}.
Common situations: 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.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
AI-assisted analysis of donnemartin/interactive-coding-challenges@358f2cc604 (2026-08-28).
Data as JSON: /api/errors/0a7eb3461fbb14d7.
Report an issue: GitHub.