KatInterview

Simulate a Task Board with Id-Keyed Commands

Data Structure DesignAuto-graded · 19 test cases
EasyAuto-gradedData Structure Designhash-mapsimulationdesign

Last updated

Process a batch of add, update, get and delete commands against an in-memory task store keyed by id, returning one result per command.

You are building the core of a small task board. Tasks live in memory, and each one has:

  • an integer id (unique across the board),
  • a string title,
  • a string status.

Instead of calling methods on an object, you receive a whole batch of commands to run in order, starting from an empty board. Write a function that executes them and returns the result of each command.

You are given two lists of equal length:

  • commands[i] is the name of the i-th command.
  • params[i] is the list of arguments for that command.

Supported commands

Commandparams[i]BehaviorResult
"addTask"[id, title]If a task with this id is already on the board, do nothing and return False. Otherwise create the task with status "PENDING" and return True.bool
"updateStatus"[id, status]If no task has this id, return False. Otherwise set the task's status to the given string, exactly as provided, and return True.bool
"getTask"[id]If the task exists, return a dictionary with the keys "id", "title" and "status" describing its current state. Otherwise return None.dict or None
"deleteTask"[id]If no task has this id, return False. Otherwise remove the task and return True.bool

Rules

  • The returned list contains exactly one entry per command, in the same order as commands.
  • A failed addTask must leave the existing task untouched (its title and status do not change).
  • After a task is deleted, its id is free again and can be added anew, starting at "PENDING".
  • The status string in updateStatus is stored as given. You do not need to check it against a list of allowed values.
  • The dictionary returned by getTask is a snapshot. Later commands must not change a dictionary that was already returned.

Constraints

  • len(commands) == len(params)
  • Every command name is one of "addTask", "updateStatus", "getTask", "deleteTask".
  • Each params[i] has the argument count shown in the table above.
  • Every id is a non-negative integer.
  • Titles and statuses are strings.
  • Aim for O(1) expected time per command.

Examples

Example 1

Input: run_task_board(['addTask', 'updateStatus', 'getTask', 'addTask'], [[1, 'Finish report'], [1, 'COMPLETED'], [1], [1, 'Duplicate']])

Output: [True, True, {'id': 1, 'title': 'Finish report', 'status': 'COMPLETED'}, False]

Example 2

Input: run_task_board(['addTask', 'getTask', 'deleteTask', 'getTask'], [[7, 'Buy groceries'], [7], [7], [7]])

Output: [True, {'id': 7, 'title': 'Buy groceries', 'status': 'PENDING'}, True, None]

Example 3

Input: run_task_board(['getTask', 'updateStatus', 'deleteTask', 'addTask'], [[99], [99, 'IN_PROGRESS'], [99], [0, 'New task']])

Output: [None, False, False, True]

Hints

Hint 1

You need to find, insert and remove tasks by id quickly. Which built-in structure gives expected constant time for all three?

Hint 2

Decide up front what to store per task and what to return from getTask. Returning a copy of the stored record keeps later commands from altering earlier results.

Hint 3

Handle each command by checking whether the id is present first. A failed add must not overwrite anything, and update and delete must report False for a missing id.