Simulate a Task Board with Id-Keyed Commands
Data Structure DesignAuto-graded · 19 test casesLast 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
| Command | params[i] | Behavior | Result |
|---|---|---|---|
"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
addTaskmust leave the existing task untouched (its title and status do not change). - After a task is deleted, its
idis free again and can be added anew, starting at"PENDING". - The status string in
updateStatusis stored as given. You do not need to check it against a list of allowed values. - The dictionary returned by
getTaskis 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
idis 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.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Keep a hash map from id to a small record {"id", "title", "status"}. Walk through the commands in order and handle each one against the map:
- addTask: if the id is already a key, append
Falseand change nothing. Otherwise insert a record with status"PENDING"and appendTrue. - updateStatus: if the id is missing, append
False. Otherwise overwrite the record'sstatuswith the given string and appendTrue. - getTask: look up the id. If found, append a shallow copy of the record. If not, append
None. - deleteTask: if the id is missing, append
False. Otherwise remove the key and appendTrue.
Why it is correct
- Uniqueness: the map has at most one entry per id, and
addTaskinserts only when the key is absent. A duplicate add therefore fails and leaves the old record intact. - Existence rules: every command checks the map before acting, so a missing id always gives
False(orNoneforgetTask), exactly as specified. - Re-adding after delete: deleting removes the key entirely, so a later
addTaskwith that id sees no entry and creates a fresh"PENDING"task. - Snapshots:
getTaskreturns a copy. Later updates modify only the stored record, so results already in the output list never change. - Order: results are appended as commands are processed, so the output matches the command order one to one.
Complexity
- Time: O(1) expected per command, so O(m) for m commands. Each record has only three fields, so copying it is constant work.
- Space: O(n) for the n tasks on the board at any moment, plus O(m) for the output list.
A note on concurrency
The original design problem asks for a thread-safe class. This version processes commands sequentially, so no locking is needed. In a real multi-threaded service you would guard the check-then-insert and lookup-then-mutate steps with a lock, so each command stays atomic.
Reference solution
from typing import List
def run_task_board(commands: List[str], params: List[List]) -> List:
board = {}
out = []
for cmd, args in zip(commands, params):
if cmd == "addTask":
task_id, title = args
if task_id in board:
out.append(False)
else:
board[task_id] = {
"id": task_id,
"title": title,
"status": "PENDING",
}
out.append(True)
elif cmd == "updateStatus":
task_id, status = args
if task_id not in board:
out.append(False)
else:
board[task_id]["status"] = status
out.append(True)
elif cmd == "getTask":
task = board.get(args[0])
out.append(dict(task) if task is not None else None)
elif cmd == "deleteTask":
task_id = args[0]
if task_id not in board:
out.append(False)
else:
del board[task_id]
out.append(True)
return out