Introduction
Calendars are built on one small idea: two intervals [a, b) and [c, d) overlap exactly when a < d && c < b. This scheduler uses it for conflict detection, a std::priority_queue for "what's next?", and a sweep over sorted bookings to find free time.
Problem statement
add books an event at a day and time for a number of minutes, rejecting overlaps (and naming the conflicting event), events past the end of the week and events that cross midnight. cancel removes an event. agenda lists a day or the whole week with total booked time. next finds the next event from a given moment. free lists gaps of at least N minutes within working hours (09:00–18:00).Objectives
- Represent time as minutes since a reference point
- Detect interval overlaps
- Use std::priority_queue as a min-heap with std::greater<>
- Implement lazy deletion for a heap
Features
- Conflict detection naming the clashing event
- Agenda per day or for the week
- Next-event queries from any time
- Free-slot search inside working hours
Concepts demonstrated
- Interval arithmetic
- Priority queues
- Lazy deletion
- Sweep algorithms
- std::optional
System architecture
Times are minutes since Monday 00:00. events_ (id → Event) is the source of truth; reminders_ is a min-heap of (start, id). Because a heap can't remove arbitrary entries, cancel only erases from the map and next pops cancelled ids lazily when they reach the top; skipping past events is done on a copy of the heap so earlier questions still work. free_slots sorts the day's bookings and sweeps a cursor from 09:00, reporting every gap long enough before moving the cursor to the end of each booking.
Folder structure
Complete source code
Makefile
What each file does
- src/main.cpp
- Time parsing and formatting, the
Schedulerclass (add with overlap check, cancel, next, agenda, free slots) and the command loop. - Makefile
- Build automation:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
add Mon 09:00 60 Team stand-up and planning add Mon 10:30 90 Data structures lecture add Mon 09:45 30 Quick call add Mon 10:00 30 Coffee with mentor add Mon 14:00 120 Lab: linked lists add Tue 09:30 45 Gym add Wed 23:30 60 Late study add Fri 16:00 60 Project demo add Xyz 10:00 30 Bad day agenda Mon free Mon 45 free Mon 120 next Mon 09:15 next Mon 15:00 cancel 3 next Mon 09:15 agenda next Fri 17:30 quit
Testing
- Compare
./app < tests/sample-input.txtwithtests/expected-output.txt. - The 09:45 call clashes with the 09:00–10:00 stand-up; the 10:00 coffee fits exactly between two events.
free Mon 45finds 12:00–14:00 and 16:00–18:00; 09:00–12:00 is fully booked.- After cancelling the coffee,
next Mon 09:15returns the 10:30 lecture.
Common errors
Back-to-back events are reported as conflicts
Use half-open intervals: an event ending at 10:00 and one starting at 10:00 do not overlap (<, not <=).
Cancelled events still show up as next
Check that the id at the top of the heap still exists before using it (lazy deletion).
Possible improvements
- Recurring weekly events
- Several calendars and a 'find a time for everyone' command
- Export the week as an iCalendar file
Additional challenges
- 1Let events cross midnight by splitting them.
- 2Suggest the nearest free slot when a booking conflicts.
- 3Add reminders N minutes before each event.
Built and tested it yourself? Mark it to update your dashboard.