Design a task system with assignments
InstacartSoftware Engineer · Technical ScreenLast updated
This question tests your ability to design a flexible data model for a task-assignment system, focusing on efficient lookups and state management in an in-memory environment.
Task Assignment System
Design an in-memory task assignment system that manages tasks and their assignments to workers. You are required to support the following operations efficiently:
create_task(task_id, priority): Registers a new task with a given ID and integer priority.assign_task(task_id, worker_id): Assigns an existing task to a specific worker. A task can only be assigned to one worker at a time.get_tasks_for_worker(worker_id): Returns all tasks currently assigned to a given worker, ordered by priority (highest priority first).complete_task(task_id): Marks a task as completed and removes it from the system.
Constraints
task_idandworker_idare unique integers.- Priority is an integer (higher value means higher priority).
- The system should handle up to 100,000 tasks and 1,000 workers.
Examples
# Scenario
system = TaskSystem()
system.create_task(101, 5)
system.create_task(102, 10)
system.assign_task(101, 1)
system.assign_task(102, 1)
# get_tasks_for_worker(1) returns [102, 101] (Priority 10 > Priority 5)
Implementation Requirements
Please provide a class structure for TaskSystem with the methods defined above. Consider the time complexity of each operation, specifically focusing on how to maintain the order of tasks per worker.
Hints
Hint 1
Use a hash map to store task metadata and another to map workers to their respective sets of assigned tasks.
Hint 2
A standard list per worker will require re-sorting on every retrieval; consider a data structure that keeps elements sorted upon insertion.
Hint 3
For the complete_task operation, ensure you can efficiently remove the task from both the global storage and the specific worker's assignment list.
Follow-up questions
- How would you modify the system to support a worker unassigning themselves from a task?
- If multiple workers could be assigned to the same task, how would your data structures change?
- How would you handle persistence if the system needs to survive a service restart?
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.