Task Management System
Learn this problemProblem statement
Implement a task-management service by processing a finite sequence of timestamped operations in order.
Every operation begins with an opcode. Timestamps are strictly increasing.
["ADD_USER", t, userId, quota]creates a user with a maximum number of active tasks.["CREATE", t, taskId, userId, priority, dueAt, expiresAt]creates an active task. It fails if the task identifier already exists, the user does not exist, or the user's active-task quota is full.["GET", t, taskId]returns[taskId, userId, priority, createdAt, dueAt, expiresAt, status], or an empty row for a missing or deleted task.["UPDATE", t, taskId, priority, dueAt, expiresAt]updates an active task.["DELETE", t, taskId]marks an existing non-deleted task as deleted.["SEARCH", t, userId]returns that user's active task identifiers ordered by decreasing priority, then increasing creation timestamp, then identifier.["SET_QUOTA", t, userId, quota]changes a user's quota. Lowering it does not remove existing tasks, but new tasks remain blocked until the active count is below the quota.["COMPLETE", t, taskId]marks an active task completed.["HISTORY", t, userId, view]returns identifiers in increasing creation order.viewisCOMPLETED,EXPIRED,UNFINISHED, orOVERDUE. Unfinished tasks are active; overdue tasks are active tasks withdueAt < t.
Before each operation at time t, every active task with expiresAt <= t becomes expired. Completion, deletion, and expiration free an active-task quota slot.
Return one row per input operation. State-changing operations return ["OK"] on success. Failures return one of ["UNKNOWN_USER"], ["DUPLICATE_TASK"], ["QUOTA_EXCEEDED"], ["NOT_FOUND"], or ["NOT_ACTIVE"]. Search and history operations may return an empty row.
Function
processTaskOperations(operations: String[][]) → String[][]Examples
Example 1
operations = [["ADD_USER","1","u1","2"],["CREATE","2","t1","u1","5","10","20"],["CREATE","3","t2","u1","7","4","30"],["SEARCH","5","u1"],["COMPLETE","6","t2"],["HISTORY","7","u1","COMPLETED"],["SET_QUOTA","8","u1","1"],["CREATE","9","t3","u1","9","12","40"],["GET","20","t1"]]return = [["OK"],["OK"],["OK"],["t2","t1"],["OK"],["t2"],["OK"],["QUOTA_EXCEEDED"],["t1","u1","5","2","10","20","EXPIRED"]]The higher-priority task is returned first. Completing it frees a quota slot, but the later quota reduction leaves one active task, so the next creation is rejected. The final read first expires t1.
Constraints
1 <= operations.length <= 200000- All timestamps and task time fields are integers in
[0, 10^9]; operation timestamps are strictly increasing. - For every CREATE or UPDATE,
operation timestamp < dueAt < expiresAt. - User and task identifiers are nonempty ASCII strings of at most 40 characters.
- Priority is an integer in
[-10^9, 10^9]; quota is in[0, 200000]. - ADD_USER uses a new user identifier. SET_QUOTA, SEARCH, and HISTORY name an existing user.
- The total number of task records examined by all SEARCH and HISTORY operations is at most 200000.