KatInterview

Simulate a Task Management Engine with CRUD Operations

Data Structure DesignAuto-graded · 23 test cases
EasyAuto-gradedData Structure Designdesign-dsarraysdata-structures

Last updated

Simulate an in-memory task tracker handling creation, retrieval, updates, filtering, and deletion of tasks with valid state validation.

Design an in-memory task manager that processes a sequence of standard CRUD operations on tasks. Each task is represented by a dictionary containing three keys:

  • "id": a unique positive integer identifier.
  • "title": a string specifying the task title.
  • "status": a string indicating the current status, which must be one of "new", "pending", or "resolved".

The engine starts empty. Task identifiers are assigned sequentially starting from 1 (i.e., 1, 2, 3, ...) and incremented every time a new task is created. IDs are never reused, even if tasks are deleted.

You are given a list of command strings operations and a corresponding list of argument lists arguments. Process each operation in order and record its output in a returned list.

Supported Operations

  1. create: args = [title]

    • Creates a new task with the given title and default status "new".
    • Assigns the next available auto-incremented ID.
    • Appends a copy of the created task object to the results list.
  2. get: args = [task_id]

    • Retrieves the task dictionary corresponding to task_id.
    • Appends a copy of the task object if found, or None if no task exists with that ID.
  3. list: args = [] or args = [status_filter]

    • If args is empty or args[0] is None, retrieves all currently existing tasks.
    • If args[0] is a string status, retrieves all existing tasks matching that status.
    • If args[0] is a non-None string that is not one of "new", "pending", or "resolved", raises a ValueError.
    • Appends a list of copies of all matching task dictionaries to the results list, ordered by task ID ascending.
  4. update: args = [task_id, new_title, new_status]

    • Finds the task with ID task_id. If the task does not exist, appends None to the results list without making changes.
    • If new_title is not None, updates the task's title.
    • If new_status is not None, updates the task's status. If new_status is not one of "new", "pending", or "resolved", raises a ValueError.
    • If successful, appends a copy of the updated task object to the results list.
  5. delete: args = [task_id]

    • Removes the task with ID task_id if it exists.
    • Appends True if a task was found and deleted, or False if the ID did not exist.

Constraints

  • operations and arguments have the exact same length.
  • Operations will only be one of "create", "get", "list", "update", or "delete".
  • Task IDs are positive integers.
  • If an operation raises a ValueError, raising the standard Python ValueError exception is expected.

Examples

Example 1

Input: task_manager(['create', 'create', 'get', 'list', 'update', 'list', 'delete', 'get'], [['Buy milk'], ['Write report'], [1], [], [1, 'Buy oat milk', 'pending'], ['pending'], [2], [2]])

Output: [{'id': 1, 'title': 'Buy milk', 'status': 'new'}, {'id': 2, 'title': 'Write report', 'status': 'new'}, {'id': 1, 'title': 'Buy milk', 'status': 'new'}, [{'id': 1, 'title': 'Buy milk', 'status': 'new'}, {'id': 2, 'title': 'Write report', 'status': 'new'}], {'id': 1, 'title': 'Buy oat milk', 'status': 'pending'}, [{'id': 1, 'title': 'Buy oat milk', 'status': 'pending'}], True, None]

Example 2

Input: task_manager(['create', 'update', 'get', 'delete', 'get', 'list'], [['Fix bug'], [1, None, 'resolved'], [1], [1], [1], []])

Output: [{'id': 1, 'title': 'Fix bug', 'status': 'new'}, {'id': 1, 'title': 'Fix bug', 'status': 'resolved'}, {'id': 1, 'title': 'Fix bug', 'status': 'resolved'}, True, None, []]

Example 3

Input: task_manager(['list', 'create', 'list', 'update', 'list'], [[], ['Read book'], ['new'], [1, 'Read entire book', 'resolved'], ['resolved']])

Output: [[], {'id': 1, 'title': 'Read book', 'status': 'new'}, [{'id': 1, 'title': 'Read book', 'status': 'new'}], {'id': 1, 'title': 'Read entire book', 'status': 'resolved'}, [{'id': 1, 'title': 'Read entire book', 'status': 'resolved'}]]

Hints

Hint 1

Use a hash map (dictionary) mapping `task_id` to task objects for $O(1)$ lookups, along with an integer counter for ID generation.

Hint 2

Make sure to return copies of task dictionaries so that subsequent updates do not retroactively alter previously returned objects in the result list.

Hint 3

Validate that any provided `status` string belongs to the set `{'new', 'pending', 'resolved'}` when processing `list` or `update` operations.