Design a Todo List - Problem
Design a Todo List
You need to design a comprehensive todo list system that supports multiple users and advanced task management features. Each user should be able to:
- Add tasks with descriptions, due dates, and tags
- Mark tasks as complete when finished
- View pending tasks sorted by due date
- Filter tasks by specific tags for better organization
Your TodoList class must efficiently handle concurrent users while maintaining data integrity and providing fast query responses.
Class Methods to Implement:
TodoList()- Initialize the todo list systemint addTask(int userId, String taskDescription, int dueDate, List<String> tags)- Add a new task and return its unique IDList<String> getAllTasks(int userId)- Get all pending tasks for a user, ordered by due dateList<String> getTasksForTag(int userId, String tag)- Get pending tasks filtered by tag, ordered by due datevoid completeTask(int userId, int taskId)- Mark a task as completed
Example Usage:
TodoList todoList = new TodoList();
int taskId = todoList.addTask(1, "Buy groceries", 20231201, ["shopping", "urgent"]);
todoList.getAllTasks(1); // Returns: ["Buy groceries"]
todoList.completeTask(1, taskId);
todoList.getAllTasks(1); // Returns: [] Input & Output
example_1.py โ Basic Operations
$
Input:
todoList = TodoList()
taskId1 = todoList.addTask(1, "Buy groceries", 20231201, ["shopping", "urgent"])
taskId2 = todoList.addTask(1, "Walk the dog", 20231130, ["pets", "daily"])
print(todoList.getAllTasks(1))
print(todoList.getTasksForTag(1, "urgent"))
โบ
Output:
["Walk the dog", "Buy groceries"]
["Buy groceries"]
๐ก Note:
Tasks are returned sorted by due date. Walk the dog (20231130) comes before Buy groceries (20231201). The tag filter returns only tasks with the 'urgent' tag.
example_2.py โ Task Completion
$
Input:
todoList = TodoList()
taskId = todoList.addTask(1, "Finish project", 20231215, ["work"])
print(todoList.getAllTasks(1))
todoList.completeTask(1, taskId)
print(todoList.getAllTasks(1))
โบ
Output:
["Finish project"]
[]
๐ก Note:
After completing a task, it no longer appears in the pending tasks list returned by getAllTasks or getTasksForTag.
example_3.py โ Multiple Users
$
Input:
todoList = TodoList()
todoList.addTask(1, "User 1 Task", 20231201, ["personal"])
todoList.addTask(2, "User 2 Task", 20231201, ["personal"])
print(todoList.getAllTasks(1))
print(todoList.getAllTasks(2))
print(todoList.getTasksForTag(2, "personal"))
โบ
Output:
["User 1 Task"]
["User 2 Task"]
["User 2 Task"]
๐ก Note:
Each user has their own separate task list. Tasks are isolated by user ID, so user 1 only sees their tasks and user 2 only sees theirs.
Constraints
- 1 โค userId, taskId โค 100
- 0 โค dueDate โค 108
- 1 โค taskDescription.length โค 50
- 0 โค tags.length โค 100
- 1 โค tags[i].length โค 10
- At most 100 calls will be made to addTask
- At most 100 calls will be made to getAllTasks
- At most 100 calls will be made to getTasksForTag
- At most 100 calls will be made to completeTask
Visualization
Tap to expand
Understanding the Visualization
1
User Drawer
Each user has their own drawer with tasks sorted by due date
2
Tag Filing System
Cross-reference system where tasks are also filed by tag combinations
3
Central Registry
Master list of all task details with completion status
4
Quick Retrieval
Any query can be answered by going directly to the right drawer
Key Takeaway
๐ฏ Key Insight: By maintaining multiple specialized indexes (user-based and tag-based), we can answer any query in O(1) time plus the time to sort the specific user's tasks, which is much faster than scanning all tasks in the system.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code