Implement multi-level task manager APIs
RampSoftware Engineer · Online AssessmentLast updated
This question evaluates implementation skills for stateful in-memory data structures, API evolution handling, substring search and prioritized sorting, and time-windowed assignment logic with quota enforcement in the Coding & Algorithms domain, and is primarily a practical application problem rather than a purely conceptual one.
You are implementing an in-memory task management system with a set of APIs that evolve over 4 levels. All APIs receive a timestamp: int parameter. Unless otherwise stated, timestamps are integers and can be assumed to be non-decreasing across calls (you may state and rely on this if needed).
Task IDs are generated sequentially as strings:
- First task is
"task_id_1", then"task_id_2", etc. - Creation order is the numeric suffix order (e.g.,
task_id_2comes beforetask_id_10).
A task has:
name: strpriority: int(non-negative,>= 0)
Assignments (introduced later) are time-based.
Level 1: Basic task CRUD
Implement the following:
add_task(self, timestamp: int, name: str, priority: int) -> str
- Adds a new task with the given
nameandpriority. - Returns the unique task ID.
- Adding multiple tasks with the same
nameandpriorityis allowed; tasks are differentiated by ID. - Note:
timestampis unused in Level 1 logic; included for API consistency.
update_task(self, timestamp: int, task_id: str, name: str, priority: int) -> bool
- Updates the
nameandpriorityof the task identified bytask_id. - Returns
Trueif update succeeds;Falseiftask_iddoes not exist.
get_task(self, timestamp: int, task_id: str) -> str | None
- Retrieves a task by
task_id. - If it exists, return a JSON-formatted string containing the task details (
nameandpriority). - If it does not exist, return
None.
Level 2: Searching and sorting tasks
Add the following APIs:
search_tasks(self, timestamp: int, name_filter: str, max_results: int) -> list[str]
- Finds tasks whose
namecontainsname_filteras a case-sensitive substring. - Returns up to
max_resultsmatching task IDs. - Sort results by:
prioritydescending, then- creation order ascending.
- If
max_results <= 0, return[].
list_tasks_sorted(self, timestamp: int, limit: int) -> list[str]
- Returns up to
limittask IDs across the system. - Sorting is the same:
prioritydescending, then creation order ascending.
Level 3: Users and time-based task assignments
Introduce users with quotas and time-windowed assignments.
add_user(self, timestamp: int, user_id: str, quota: int) -> bool
- Adds a new user with a maximum number of simultaneously active assignments (
quota). - Returns
Trueif added,Falseifuser_idalready exists.
assign_task(self, timestamp: int, task_id: str, user_id: str, finish_time: int) -> bool
- Assigns
task_idtouser_idfor the time interval [timestamp, finish_time). - The assignment is active at time
tiffstart_time <= t < finish_time. - Returns
Trueif successful; otherwiseFalseif: - the task does not exist, or
- the user does not exist, or
- the user's quota is already fully used by currently active assignments.
- Each successful active assignment consumes one quota slot while it remains active.
get_user_tasks(self, timestamp: int, user_id: str) -> list[str]
- Returns all active task IDs assigned to
user_idat the giventimestamp. - Only include assignments where
start_time <= timestamp < finish_time.
Level 4: Completion and overdue detection
Add completion and overdue queries.
complete_task(self, timestamp: int, task_id: str, user_id: str) -> bool
- Marks a task as completed by
user_idat timetimestamp. - The task must be assigned to the user and active at
timestamp. - When completed, the user's quota slot is freed immediately.
- Returns
Trueif successful; otherwiseFalseif: - task does not exist, or
- user does not exist, or
- task is not assigned to the user at
timestamp.
get_overdue_assignments(self, timestamp: int, user_id: str) -> list[str]
- Returns task IDs for assignments that were given to
user_idand expired without completion. - An assignment is overdue if:
finish_time <= timestamp, and- the task was not completed before the
finish_timeof that specific assignment.
Implement the full system so that all levels work together consistently. Clarify any additional assumptions you need (e.g., whether timestamps are non-decreasing, whether a task can be assigned multiple times, and how repeated assignments to the same user/task should behave), but your behavior must remain consistent with the definitions above.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.