Anthropic Online Assessment -- Task Management System
Requirements
Level 1
Implement a simple task management system that handles tasks with CRUD operations.
- addTask(taskId: String, priority: Int) -> Boolean: Adds a task. Returns false if the taskId already exists; otherwise, returns true.
- updateTask(taskId: String, newPriority: Int) -> Boolean: Updates the priority of an existing task. Returns false if the task is not found.
- getTask(taskId: String) -> Task | null: Returns the task details.
Examples
addTask("task1", 10);
addTask("task1", 20);
updateTask("task2", 5);
getTask("task1");
Explanations
returns true because "task1" is new.
returns false because "task1" already exists.
returns false because "task2" was never added.
returns 10, the current priority of "task1".
Level 2
Implement Search and Sorting functions based on task priority.
- searchTasks(minP: Int, maxP: Int) -> List<String>: Returns a list of taskIds where minP <= priority <= maxP.
- Sorting Requirement: Results must be sorted by priority (descending).
Tie-breaking: If priorities are equal, sort by creation order (descending)—meaning the task added most recently appears first.
Examples
addTask("task1", 10);
addTask("task2", 20);
addTask("task3", 20);
addTask("task4", 5);
searchTasks(10, 25);
Explanations
returns ["task3", "task2", "task1"] because:
- "task3" and "task2" both have priority 20, but "task3" was created after "task2".
- "task1" has priority 10, which is inside the range [10, 25].
- "task4" is excluded (priority 5 is too low).
Level 3
Introduce user ownership, time-sensitive expiration, and resource limits.
addUser(userId: String, quota: Int) -> Boolean: Registers a user with a maximum number of active tasks. Returns false if the userId already exists.
assignTask(taskId: String, userId: String, priority: Int, ttl: Int, timestamp: Int) -> Boolean:
Checks if the user exists and has not exceeded their quota.
Calculates expiration: Task expires at timestamp + ttl.
Returns false if the user is at quota, the user doesn't exist, or the taskId already exists.
Note: Before processing, the system must expire any tasks whose timestamp + ttl <= current_timestamp.
Examples
addUser("user1", 1);
assignTask("task1", "user1", 10, 5, 100);
assignTask("task2", "user1", 20, 5, 102);
assignTask("task2", "user1", 20, 5, 105);
Explanations
returns true. "user1" is created with a quota of 1.
returns true. "task1" assigned at T=100 (expires T=105). Quota is 1/1.
returns false. At T=102, "task1" is still active. User1 is at quota.
returns true. At T=105, "task1" expires (100+5 <= 105). Quota becomes 0/1, allowing "task2" to be added.
Level 4
Track the lifecycle of tasks to differentiate between successful completion and expiration.
completeTask(taskId: String, finishTime: Int) -> Boolean: Marks a task as "Completed." Returns false if the task does not exist, has already expired (based on finishTime), or was already completed. If successful, the user’s active task count decreases, freeing up quota.
getOverdueTasks() -> List<String>: Returns a list of taskIds that transitioned to an "Expired" state without being completed.
Examples
addUser("user2", 2);
assignTask("task_A", "user2", 10, 10, 200);
assignTask("task_B", "user2", 10, 10, 205);
completeTask("task_A", 208);
completeTask("task_B", 220);
getOverdueTasks();
Explanations
returns true. "user2" created.
returns true. "task_A" expires at 210.
returns true. "task_B" expires at 215.
returns true. Finish time 208 is before expiry 210. Task is completed.
returns false. Finish time 220 is after expiry 215. Task is already expired.
returns ["task_B"] because "task_B" expired at T=215 without being completed.
For more questions, please checkout our Crash Course.
