You are building an in-memory worker management module. Each worker has metadata and a history of office attendance intervals. Implement the following operations.
Each worker has:
workerId
(string or int, unique)
title
(string)
salary
(number): assume this is a pay rate per unit time (e.g., dollars per hour). Pay is accrued
only while the worker is in the office
.
Time is given as an integer timestamp (monotonic within a single worker’s timeline). Attendance is tracked by toggling in/out:
registerTime
is the
start
of a new in-office interval.
registerTime
is the
end
of the current in-office interval.
Assume inputs are valid unless otherwise stated, but your design should define behavior clearly for edge cases.
addWorker(workerId, title, salary)
registerTime(workerId, timestamp)
getTotalInOfficeTime(workerId) -> totalTime
topNByInOfficeTime(n) -> list
n
workers ranked by
total accumulated in-office time
.
topNByInOfficeTimeForTitle(n, title) -> list
title
.
Define your output format (e.g., list of (workerId, totalTime)), and specify tie-breaking.
promote(workerId, newTitle, newSalary)
getPay(workerId, startTime, endTime) -> pay
[startTime, endTime)
.
[startTime, endTime)
and multiply the overlap duration by the salary rate that was effective during that time.
registerTime
timestamps are guaranteed non-decreasing per worker.
getTotalInOfficeTime
or
getPay
.
workerId
, duplicate
addWorker
, and
startTime >= endTime
.
topN*
and
getPay
).