KatInterview

Task Assignment System with Expiration Windows

Data Structure DesignAuto-graded · 21 test cases
MediumAuto-gradedData Structure Designobject-oriented-designtime-based-logicdata-structures

Last updated

Tests in-memory data modeling, time-based state management, priority queuing, and handling overlapping task assignments with TTL semantics.

Build an in-memory system that manages tasks and user assignments with time-to-live (TTL) behavior.

Core Entities

Task: identified by a unique id, has a name and a numeric priority.

User: identified by a unique id, has a quota limiting how many assignments can be simultaneously active.

Assignment: links a user to a task starting at startTime for a duration ttl. The assignment is considered active during the half-open interval [startTime, startTime + ttl) and becomes expired at startTime + ttl or later. An assignment can be marked completed, after which it no longer counts as active or expired.

Operations

Your implementation receives a sequence of operations and returns results for each.

Task Management

  • createTask(task_id, name, priority): Create a new task. Return False if task_id already exists, otherwise True.
  • getTask(task_id): Return a dictionary {"id": task_id, "name": name, "priority": priority} or None if the task does not exist.
  • updateTask(task_id, updates): Update fields in updates (a dict with keys "name" and/or "priority"). Return False if the task does not exist, otherwise True.

Querying Tasks

  • topTasks(n): Return the top n tasks by priority (higher priority first), breaking ties by id in ascending lexicographic order. Return a list of task dictionaries.
  • searchTasks(substring, n): Same as topTasks, but only consider tasks whose name contains substring as a substring.

User and Assignment Management

  • addUser(user_id, quota): Register a user with a maximum active assignment limit. Return False if user_id already exists, otherwise True.

  • assignTask(user_id, task_id, start_time, ttl): Assign a task to a user starting at start_time for duration ttl. The assignment fails (return False) if:

    • The user or task does not exist
    • ttl <= 0
    • At time start_time, the user already has a number of active assignments equal to or exceeding their quota

    Otherwise, create the assignment and return True. Multiple assignments of the same task to the same user are allowed and treated as distinct.

  • activeTasks(user_id, time): Return a list of task IDs for all active assignments for this user at the given time, sorted by (startTime, taskId) ascending.

  • completeTask(user_id, task_id, time): Mark an assignment complete. Among all active assignments for this (user_id, task_id) pair at time, complete the one with the earliest startTime. Return True if an assignment was completed, False if no active assignment exists for this pair.

  • expiredTasks(user_id, time): Return a list of task IDs for all expired (but not completed) assignments for this user at the given time, sorted by (startTime, taskId) ascending.

Constraints

  • All IDs are non-empty strings or integers
  • priority is an integer
  • quota >= 0
  • n >= 0
  • ttl, start_time, and time are non-negative integers
  • Time values are not necessarily monotonically increasing across operations
  • At most 10⁴ operations
  • At most 10³ tasks, 10³ users, and 10⁴ assignments total

Examples

Example 1

Input: task_assignment_system(['createTask', 'createTask', 'addUser', 'assignTask', 'activeTasks', 'completeTask', 'activeTasks', 'expiredTasks'], [[1, 'Write tests', 5], [2, 'Review code', 3], ['u1', 2], ['u1', 1, 10, 5], ['u1', 12], ['u1', 1, 14], ['u1', 14], ['u1', 15]])

Output: [True, True, True, True, [1], True, [], []]

Example 2

Input: task_assignment_system(['createTask', 'createTask', 'topTasks', 'searchTasks', 'updateTask', 'getTask'], [[2, 'Pay invoice', 4], [1, 'Invoice review', 4], [2], ['Invoice', 5], [2, {'priority': 7}], [2]])

Output: [True, True, [{'id': 1, 'name': 'Invoice review', 'priority': 4}, {'id': 2, 'name': 'Pay invoice', 'priority': 4}], [{'id': 1, 'name': 'Invoice review', 'priority': 4}], True, {'id': 2, 'name': 'Pay invoice', 'priority': 7}]

Hints

Hint 1

An assignment is active during [startTime, startTime + ttl) — note the half-open interval. Expiration begins exactly at startTime + ttl.

Hint 2

Track assignments per user to efficiently count active ones at a given timestamp and to locate candidates for completion.

Hint 3

Sorting tasks by priority is straightforward with a key function; avoid maintaining a persistent sorted structure unless queries dominate.