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
Design a Todo List - Hash Map Approach INPUT User 1 Tasks: taskId: 1 desc: "Buy groceries" dueDate: 20231201 tags: ["shopping","urgent"] PENDING taskId: 2 desc: "Walk the dog" dueDate: 20231130 tags: ["pets","daily"] PENDING Data Structure: HashMap<userId, List<Task>> HashMap<taskId, Task> HashMap<tag, List<Task>> ALGORITHM STEPS 1 addTask() Store task in user's list Index by taskId, tags 2 getAllTasks() Filter pending tasks Sort by dueDate ASC 3 getTasksForTag() Lookup tag index Filter + Sort by date 4 completeTask() Mark completed=true O(1) via taskId map Sort by dueDate: 20231130 (Nov 30) 20231201 (Dec 01) First Second FINAL RESULT getAllTasks(1): [ "Walk the dog", "Buy groceries" ] OK getTasksForTag(1, "urgent"): [ "Buy groceries" ] OK Time Complexity: addTask(): O(1) getAllTasks(): O(n log n) getTasksForTag(): O(m log m) completeTask(): O(1) Sorted by dueDate ascending Nov 30 comes before Dec 1 Key Insight: Hash Map Multi-Indexing Use multiple hash maps to enable O(1) lookups: userId --> tasks, taskId --> task, tag --> tasks. This trades space for time, allowing fast retrieval while maintaining task relationships. Sort only when querying (lazy evaluation) to avoid repeated sorting on every add operation. TutorialsPoint - Design a Todo List | Hash Map Approach
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