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 system
  • int addTask(int userId, String taskDescription, int dueDate, List<String> tags) - Add a new task and return its unique ID
  • List<String> getAllTasks(int userId) - Get all pending tasks for a user, ordered by due date
  • List<String> getTasksForTag(int userId, String tag) - Get pending tasks filtered by tag, ordered by due date
  • void 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
User IndexUser 1User 2Sorted Task ListsTag Index(U1,urgent)(U2,work)Tag-filtered ListsTask StorageT1T2T3T4T5O(1) User
LookupO(1) Tag
Filtering
๐ŸŽฏ Multiple indexes enable O(1) access patterns for all query types
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.
Asked in
Google 35 Microsoft 28 Amazon 25 Meta 20
42.8K Views
High Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen