← 返回 instacart 的题目列表OA: Worker Attendance & Payroll
类型:qbank
A newer CodeSignal OA variant. Build a worker registry, alternate entry/exit timestamps to compute total office time, rank top workers by time in office, support promotions that take effect only on the next office entry, and compute pay over a time range.
Requirements
Level 1:
addWorker(id, position, compensation) creates a worker with metadata.
register(id, timestamp) records a visit boundary. If the worker is outside the office, the timestamp is an entry; if already inside, it is an exit.
get(id) returns the worker's total time spent in the office across completed sessions.
Level 2:
topN(n) returns workers ranked by total office time.
topN(n, position) returns only workers whose current position matches the given position, still ranked by all completed office time.
Tie-breakers are usually lexicographic by worker id; confirm from visible tests.
Level 3:
promote(id, newPosition, newCompensation, timestamp) schedules a title and compensation change.
Promotion only takes effect when the worker is not in the office and next enters. If the worker is currently in office, queue it until the next entry.
If multiple promotions are queued, only the latest pending promotion matters.
calcSalary(id, start, end) computes total compensation earned over the interval.
Level 4 was not fully exposed in the available prompt, so do not assume extra behavior beyond the first three levels.
Notes
Model each worker as {position, compensation, inside, entryTime, totalTime, sessions, pendingPromotion}.
Store sessions as (start, end, position, compensation) so salary queries can intersect an arbitrary time range with historical compensation states.
register should apply a pending promotion at the moment of an entry, before opening the new session. Do not apply it while closing an existing session.
calcSalary is an interval-overlap problem: for each completed session, add max(0, min(end, sessionEnd) - max(start, sessionStart)) * sessionCompensation.
If salary may include an active open session, clarify whether end should close the interval virtually. CodeSignal tests usually use completed sessions, but a robust helper can handle both.
Common failure mode: mixing current position with historical session compensation. Ranking uses current position; salary uses historical compensation.
Preparation
Write the entry/exit state machine first and test register with in, out, in, out.
Add ranking only after totalTime is correct.
Drill interval-overlap salary calculation with a promotion that takes effect between two sessions.
Read tests carefully for tie-break and whether topN output is id(time) or a structured array.