KatInterview

Work Queue Tracker with Owners, Closing, and Late Lookups

Data Structure DesignAuto-graded · 16 test cases
MediumAuto-gradedData Structure Designdesignhash-mapsorting

Last updated

Simulate a task tracker driven by a list of operations, handling creation, reassignment, completion, and multi-key sorted queries while returning one result per operation.

Work Queue Tracker

You are building the logic of a small in-memory work tracker. Rather than writing a class, you implement one function that receives a whole script of operations and returns what each operation produced.

Task records

Every task has:

  • an id (string), unique across the tracker,
  • a description (string),
  • a priority (integer, larger means more important),
  • a deadline (integer timestamp),
  • a state, which is either "PENDING" or "COMPLETED",
  • an owner: a user id, or None when nobody holds it.

Input

  • operations: a list of operation names.
  • arguments: a list of the same length, where arguments[i] is the argument list for operations[i].

Process the operations in order, starting from an empty tracker.

Operations

OperationArgumentsResult
"create_task"[task_id, description, priority, deadline]True if a new task was added; False if a task with that id already exists (even a completed one).
"assign_task"[task_id, user_id]True if the task exists and is "PENDING", in which case its owner becomes user_id (replacing any previous owner). False if the task does not exist or is completed; nothing changes then.
"complete_task"[task_id]True if the task exists and is "PENDING", in which case it becomes "COMPLETED". False otherwise.
"get_user_tasks"[user_id, sort_by]A list of ids of tasks that are "PENDING" and owned by user_id, ordered as described below. An empty list if there are none.
"get_overdue_tasks"[current_time]A list of ids of "PENDING" tasks with deadline < current_time (strictly less), whether or not they have an owner. Sorted by deadline ascending, ties by id ascending.

New tasks start as "PENDING" with no owner.

Ordering for get_user_tasks

  • sort_by == "priority": priority descending, then deadline ascending, then id ascending.
  • sort_by == "deadline": deadline ascending, then priority descending, then id ascending.

Ids are compared as ordinary strings.

Output

Return a list with one entry per operation, in the same order as the input: a boolean for create_task, assign_task, complete_task, and a list of ids for the two query operations. Any operation name not listed above produces no entry in the output.

Constraints

  • 1 <= len(task_id), len(user_id) <= 32, alphanumeric.
  • 1 <= priority <= 10^5
  • 1 <= deadline, current_time <= 2 * 10^9
  • At most 20,000 operations in total.
  • sort_by is always "priority" or "deadline".

Examples

Example 1

Input: run_task_tracker(['create_task', 'create_task', 'create_task', 'assign_task', 'assign_task', 'assign_task', 'get_user_tasks', 'complete_task', 'get_user_tasks', 'get_overdue_tasks'], [['T1', 'Audit logs', 2, 100], ['T2', 'Fix bug', 5, 50], ['T3', 'Deploy', 2, 80], ['T1', 'alice'], ['T2', 'alice'], ['T3', 'alice'], ['alice', 'priority'], ['T2'], ['alice', 'priority'], [90]])

Output: [True, True, True, True, True, True, ['T2', 'T3', 'T1'], True, ['T3', 'T1'], ['T3']]

Hints

Hint 1

Keep one dictionary from task id to a record holding priority, deadline, state, and owner; every boolean operation is a lookup plus a state check.

Hint 2

Both the priority-first and deadline-first orderings can be expressed as a single tuple sort key by negating the priority so it sorts descending while the other fields stay ascending.

Hint 3

A completed task must be excluded from both queries, and a task that was reassigned must only show up for its latest owner. Filtering by state and current owner at query time handles this naturally.