Advanced Event Scheduler with Conflict Resolution in C++
Build an advanced event scheduler that manages multiple events with time conflicts resolution. The scheduler must support adding, removing, updating events, and querying free time slots efficiently.
Challenge prompt
Create an EventScheduler class in C++ that allows users to add, remove, and update events, each with a start and end time. When adding an event, the scheduler should detect and resolve any conflicts by automatically shifting or merging overlapping events to avoid clashes. Implement querying functionality to return available free time slots within a given time range. Optimize your implementation to handle large numbers of events efficiently.
Guidance
- • Design appropriate data structures (e.g., balanced trees or interval trees) for efficient overlap detection and event management.
- • Implement conflict resolution logic that can adjust event timings without removing events unnecessarily.
- • Provide methods for querying free time slots between events in a specified time frame.
Hints
- • Consider storing events sorted by start time for quick conflict detection.
- • Use interval tree or segment tree concepts to efficiently find conflicting events.
- • Merging events can be done by extending the end time of the earlier event if overlaps occur.
Starter code
class Event {
public:
int start;
int end;
std::string description;
Event(int s, int e, std::string desc) : start(s), end(e), description(desc) {}
};
class EventScheduler {
private:
std::vector<Event> events;
public:
bool addEvent(int start, int end, std::string description);
bool removeEvent(int start, int end);
bool updateEvent(int oldStart, int oldEnd, int newStart, int newEnd, std::string newDescription);
std::vector<std::pair<int, int>> queryFreeSlots(int startRange, int endRange);
// Implement additional helper methods as needed
};Expected output
After adding multiple events, the scheduler should manage overlapping events by merging or shifting them per the defined logic. Querying free slots between 9:00 and 17:00 may output intervals like [(9,10), (12,13), (15,17)] representing available time blocks.
Core concepts
Challenge a Friend
Send this duel to someone else and see if they can solve it.