Event Scheduler - Problem
Design an Event Scheduler system that manages events by priority and time. The system should be able to:
- Schedule new events with a given priority, start time, and duration
- Detect and handle scheduling conflicts
- Retrieve the next event to be executed
- Cancel existing events
Implement the EventScheduler class with the following methods:
schedule(eventId, priority, startTime, duration)- Schedule a new eventgetNextEvent()- Get the event with highest priority that should run nexthasConflict(startTime, duration)- Check if a time slot conflicts with existing eventscancel(eventId)- Remove an event from the schedule
Events with higher priority values should be executed first. If priorities are equal, earlier start times take precedence.
Input & Output
Example 1 — Basic Scheduling Operations
$
Input:
operations = [["schedule","E1",5,10,3],["schedule","E2",8,5,2],["getNextEvent"],["hasConflict",6,2],["cancel","E2"]]
›
Output:
["SCHEDULED","SCHEDULED","E2",true,"CANCELLED"]
💡 Note:
Schedule E1 (priority 5, time 10-13) succeeds. Schedule E2 (priority 8, time 5-7) succeeds. Next event is E2 (higher priority 8). Time 6-8 conflicts with E2 (5-7). Cancel E2 succeeds.
Example 2 — Conflict Detection
$
Input:
operations = [["schedule","A",3,15,5],["schedule","B",7,17,4],["hasConflict",14,3],["getNextEvent"]]
›
Output:
["SCHEDULED","CONFLICT",true,"A"]
💡 Note:
Schedule A (15-20) succeeds. Schedule B (17-21) conflicts with A at time 17-19. Check 14-17 conflicts with A at time 15-16. Next event is A (only active event).
Example 3 — Priority Ordering
$
Input:
operations = [["schedule","LOW",2,20,2],["schedule","HIGH",9,25,1],["schedule","MID",5,30,3],["getNextEvent"]]
›
Output:
["SCHEDULED","SCHEDULED","SCHEDULED","HIGH"]
💡 Note:
Schedule three events with different priorities (2, 9, 5). GetNextEvent returns HIGH because it has the highest priority (9).
Constraints
- 1 ≤ operations.length ≤ 1000
- Event IDs are unique strings with length ≤ 10
- 1 ≤ priority ≤ 100
- 0 ≤ startTime ≤ 106
- 1 ≤ duration ≤ 1000
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code