Problem Description
Design a Todo List system where users can add tasks with a description, due date, and optional tags. Users can then retrieve their pending tasks (ordered by due date), filter their tasks by specific tags, and mark tasks as complete. Each task is assigned a unique, sequentially increasing ID starting from 1.
Key Insights
- Use a global counter to generate sequential task IDs.
- Maintain a mapping from userIds to their active (uncompleted) tasks.
- Each task record stores its id, description, due date, and tags.
- For retrieval, sort tasks by dueDate (all dueDate values are unique).
- Filtering by tag requires checking the tags list for each task.
- Given the problem constraints (at most 100 calls per method), simple data structures and sorting on the fly are acceptable.
Space and Time Complexity
Time Complexity:
- addTask: O(1)
- getAllTasks: O(n log n) per call (n is number of uncompleted tasks for the user) due to sorting.
- getTasksForTag: O(n log n) per call in the worst case.
- completeTask: O(1)
Space Complexity:
- O(T) where T is the total number of tasks added (each user has their own mapping).
Solution
We maintain a global counter to assign unique task IDs. For each user, we store the tasks in a dictionary keyed by taskId, which holds the task details (description, dueDate, tags). When retrieving tasks (either all or by tag), we filter tasks that are uncompleted and sort them by dueDate. The completeTask function checks if the task exists for the user and marks it as completed (or removes it from the active list). Since the number of tasks is limited by the problem constraints, re-sorting during retrieval is efficient and simplifies our design.