Skip to content
C++ project #18Intermediate

Weekly Event Scheduler

Book events in a week without overlaps, find the next event with a min-heap, list a day's agenda and discover free slots of a given length.

Estimated time
~4 hours to build
Files
5 files · 370 lines
Technologies
C++17, <queue>, <map>, <optional>, <algorithm>

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

Write a scheduler for a single week. 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

TextProject tree
event-scheduler/
├── README.md
├── Makefile
├── src/main.cpp
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -O2
SRC := $(wildcard src/*.cpp)
HDR := $(wildcard src/*.h src/*.hpp)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CXX) $(CXXFLAGS) -Isrc $(SRC) -o app
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/main.cpp
Time parsing and formatting, the Scheduler class (add with overlap check, cancel, next, agenda, free slots) and the command loop.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
g++ -std=c++17 -Wall -Wextra -Isrc src/*.cpp -o app
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
=== Weekly Event Scheduler ===
> Booked #1 "Team stand-up and planning" Mon 09:00-10:00
> Booked #2 "Data structures lecture" Mon 10:30-12:00
> Conflicts with #1 "Team stand-up and planning" (Mon 09:00-10:00)
> Booked #3 "Coffee with mentor" Mon 10:00-10:30
> Booked #4 "Lab: linked lists" Mon 14:00-16:00
> Booked #5 "Gym" Tue 09:30-10:15
> Events cannot cross midnight
> Booked #6 "Project demo" Fri 16:00-17:00
> Usage: add <Day HH:MM> <minutes> <title>
> Mon
09:00-10:00 #1 Team stand-up and planning
10:00-10:30 #3 Coffee with mentor
10:30-12:00 #2 Data structures lecture
14:00-16:00 #4 Lab: linked lists
4 event(s), 5h 0m booked
> 12:00-14:00 (120 min)
16:00-18:00 (120 min)
> 12:00-14:00 (120 min)
16:00-18:00 (120 min)
> Next: "Coffee with mentor" at Mon 10:00 (in 0h 45m)
> Next: "Gym" at Tue 09:30 (in 18h 30m)
> Cancelled #3
> Next: "Data structures lecture" at Mon 10:30 (in 1h 15m)
> Mon
09:00-10:00 #1 Team stand-up and planning
10:30-12:00 #2 Data structures lecture
14:00-16:00 #4 Lab: linked lists
Tue
09:30-10:15 #5 Gym
Fri
16:00-17:00 #6 Project demo
5 event(s), 6h 15m booked
> Nothing else scheduled this week
>
Input
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.txt with tests/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 45 finds 12:00–14:00 and 16:00–18:00; 09:00–12:00 is fully booked.
  • After cancelling the coffee, next Mon 09:15 returns 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

  1. 1Let events cross midnight by splitting them.
  2. 2Suggest the nearest free slot when a booking conflicts.
  3. 3Add reminders N minutes before each event.

Built and tested it yourself? Mark it to update your dashboard.