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 event
  • getNextEvent() - Get the event with highest priority that should run next
  • hasConflict(startTime, duration) - Check if a time slot conflicts with existing events
  • cancel(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
INPUTALGORITHMRESULTEvent Operationsschedule("E1", 5, 10, 3)schedule("E2", 8, 5, 2)getNextEvent()Event TimelineE1: T10-13, P5E2: T5-7, P81Priority QueueSort by priority, then time2Conflict DetectionCheck time overlaps3Event ManagementSchedule, retrieve, cancel4Return ResultsSuccess/conflict statusExecution Results["SCHEDULED", "SCHEDULED", "E2"]Next: E2 (Priority 8)Priority Order1. E2 (P:8, T:5-7)2. E1 (P:5, T:10-13)Key Insight:Combining priority queues with efficient conflict detection enablesreal-time event scheduling with O(log n) performance per operation.TutorialsPoint - Event Scheduler | Balanced Tree Approach
Asked in
Google 45 Amazon 38 Microsoft 32 Apple 28
34.5K Views
Medium Frequency
~35 min Avg. Time
887 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen