← Questions
Office badge register that tracks worker hours and pay
0
I was asked this
No reports yet. Sign in to say where you were asked it.
Build a register for an office. Workers badge in and out; the register keeps their time in the office and works out their pay. It comes in levels; each level adds methods to the same class. Timestamps are integers and increase across calls.
Level 1: workers and time
-
add_worker(worker_id: str, position: str, compensation: int) -> bool—compensationis pay per time unit.Falseif the worker exists. -
register(worker_id: str, timestamp: int) -> str— the first call enters the office, the next leaves, and so on. Returns"registered", or"invalid_request"for an unknown worker. -
get(worker_id: str) -> int | None— total time from completed visits (in, then out);Nonefor an unknown worker.
Level 2: ranking
-
top_n_workers(n: int, position: str) -> list[str]— workers currently in that position, most time first, ties by id; each as"id(time)".
Level 3: promotions and salary
-
promote(worker_id, new_position, new_compensation, timestamp) -> str— takes effect attimestamp; time already in the office stays with the old position's total. -
calc_salary(worker_id: str, start: int, end: int) -> int— pay for time in the office inside[start, end), at the rate in force at each moment.
Level 4: bonus windows
-
add_bonus_window(start: int, end: int) -> None— time worked inside[start, end)pays double.
Example
add_worker("ana", "dev", 10) -> True
add_worker("bo", "dev", 12) -> True
register("ana", 10); register("bo", 12); register("ana", 25); register("bo", 40)
register("cy", 5) -> "invalid_request"
get("ana") -> 15 get("bo") -> 28
top_n_workers(5, "dev") -> ["bo(28)", "ana(15)"]
register("ana", 50); promote("ana", "lead", 20, 60); register("ana", 70)
calc_salary("ana", 0, 100) -> 450 # 15 × 10 + 10 × 10 + 10 × 20
calc_salary("ana", 55, 65) -> 150 # 5 × 10 + 5 × 20
top_n_workers(5, "lead") -> ["ana(35)"]
add_bonus_window(15, 30)
calc_salary("bo", 0, 100) -> 516 # 28 × 12, plus 15 × 12 doubled
Constraints
- Up to 10^5 calls; times up to 10^9.
Clarifying questions
- Does a visit still in progress count toward
getorcalc_salary? - After a promotion, does
top_n_workerscount all of the worker's time, or only time in the new position? - Can bonus windows overlap, and does overlapping double twice?
What a strong answer covers
- Per worker, a sorted list of completed visits and a sorted list of
(start, rate)changes. -
calc_salaryas interval arithmetic: clip each visit to[start, end)and split at rate changes and bonus windows; binary search to skip visits outside the range. - Top-n with a sort per query or a sorted structure per position, and the trade-off.
- Tests at boundaries: visits touching
startorend, a promotion in the middle of a visit.
Follow-ups
- Report each worker's pay for a month in one call, fast.
- Undo the last registration.
- Workers can be in several offices; badge events arrive out of order.
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
