~/problems / Intervals

Worker hours, promotions and double pay

medium 4 levels ~80 min Anthropic

Level 1 Clocking in and out

Build Office, which tracks when employees are physically at work.

  • add_employee(employee_id, position, hourly_salary) -> bool: register someone. False if the id is taken.
  • clock_in(employee_id, timestamp) -> bool: the employee arrives. False (and nothing changes) if the employee doesn't exist or is already clocked in.
  • clock_out(employee_id, timestamp) -> bool: the employee leaves, closing the session [clock_in time, timestamp). False if the employee doesn't exist or isn't clocked in.
  • get_total_work_time(employee_id) -> int | None: the summed length of all finished sessions (a session still open doesn't count yet). None for an unknown employee.

Timestamps passed to clock_in / clock_out strictly increase from call to call.

office = Office()
office.add_employee("e1", "Engineer", 10)
office.clock_in("e1", 100)
office.clock_in("e1", 120)            # False, already in
office.clock_out("e1", 150)           # True
office.get_total_work_time("e1")      # 50
office.clock_out("e1", 160)           # False, not in

Level 2 unlocks when level 1 passes.

Level 3 unlocks when level 2 passes.

Level 4 unlocks when level 3 passes.

Topic: Intervals / sweep line. Sort by start, merge; sweep events for overlaps.

Read the visual guide
0:00
Ctrl ' run · Ctrl ↵ submit
esc