PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This question evaluates the ability to design and implement in-memory data structures and algorithms for time-aware state management, including per-user task versioning, expiration (TTL), and historical queries.

  • hard
  • Anthropic
  • Coding & Algorithms
  • Software Engineer

Implement a Time-Aware Task Manager

Company: Anthropic

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

Design and implement an in-memory task management component. Each task belongs to one user. A task has a unique task ID, arbitrary task content, a creation time, and optionally an expiration time. Support the following behavior: 1. Create a task for a user at a given timestamp. 2. Return the user's current active task list at a given timestamp. 3. Set a time-to-live for a task by assigning it an expiration timestamp. Once the current time reaches that expiration timestamp, the task should no longer appear in active task lists. 4. Save or reconstruct a user's task list at any requested timestamp, so that the caller can ask: "What tasks did this user have at time `t`?" Assume timestamps are integers and method calls are made with valid timestamps. Define reasonable method signatures and data structures. Focus on correctness and on choosing data structures that avoid major refactoring when TTL and historical queries are added.

Quick Answer: This question evaluates the ability to design and implement in-memory data structures and algorithms for time-aware state management, including per-user task versioning, expiration (TTL), and historical queries.

Part 1: Basic Task Manager with Creation and Active Listing

Implement the simplest version of the task manager. You are given task creation records and user-time queries. A task is active for a user at time t if it belongs to that user and its creation time is less than or equal to t. No task ever expires in this part. For each query, return the active tasks as [task_id, content] pairs ordered by creation time; if two tasks for the same user share the same creation time, keep the order they appeared in the input arrays.

Constraints

  • 0 <= number of tasks, number of queries <= 200000
  • len(task_users) == len(task_ids) == len(task_contents) == len(created_at)
  • len(query_users) == len(query_times)
  • Each task_id is unique
  • Timestamps are integers
  • If multiple tasks for the same user have the same creation time, preserve their original input order

Examples

Input: (['u1', 'u1', 'u2'], ['t1', 't2', 't3'], ['buy milk', 'write code', 'read'], [1, 3, 2], ['u1', 'u2', 'u1'], [0, 10, 3])

Expected Output: [[], [['t3', 'read']], [['t1', 'buy milk'], ['t2', 'write code']]]

Explanation: u1 has nothing at time 0. u2 has t3 by time 10. u1 has both tasks by time 3.

Input: (['u1', 'u1', 'u1'], ['t2', 't1', 't3'], ['B', 'A', 'C'], [5, 5, 6], ['u1', 'u1'], [5, 6])

Expected Output: [[['t2', 'B'], ['t1', 'A']], [['t2', 'B'], ['t1', 'A'], ['t3', 'C']]]

Explanation: At the same creation time, the two tasks at time 5 stay in their original input order.

Input: ([], [], [], [], ['u1'], [10])

Expected Output: [[]]

Explanation: Edge case: no tasks exist.

Input: (['u1'], ['t1'], ['A'], [7], ['u2', 'u1'], [7, 6])

Expected Output: [[], []]

Explanation: A query for a different user and a query before creation both return empty lists.

Hints

  1. Group tasks by user first, instead of scanning all tasks for every query.
  2. After sorting one user's tasks by creation time, binary search tells you how many tasks already exist at a given timestamp.

Part 2: Task Manager with Expiration and TTL

Now process a chronological stream of task-manager operations. A task can be created, later assigned an expiration timestamp, and listed for a user. A task is active at time t if it has already been created and either it has no expiration time or t is strictly less than its expiration time. Return the result of every list operation as [task_id, content] pairs in create-operation order for that user.

Constraints

  • 0 <= number of operations <= 200000
  • len(op_types) == len(op_users) == len(op_task_ids) == len(op_contents) == len(op_times) == len(op_expirations)
  • op_times are strictly increasing
  • Each task_id is unique and is created before any set_ttl that refers to it
  • Each task has at most one TTL assignment
  • If a task expires at time e, it must not appear in any list at time e or later

Examples

Input: (['create', 'create', 'list', 'set_ttl', 'list', 'list'], ['u1', 'u1', 'u1', '', 'u1', 'u1'], ['t1', 't2', '', 't1', '', ''], ['A', 'B', '', '', '', ''], [1, 2, 3, 4, 5, 6], [-1, -1, -1, 6, -1, -1])

Expected Output: [[['t1', 'A'], ['t2', 'B']], [['t1', 'A'], ['t2', 'B']], [['t2', 'B']]]

Explanation: t1 gets expiration 6 at time 4. It still appears at time 5, but not at time 6.

Input: (['create', 'create', 'set_ttl', 'list', 'list'], ['u1', 'u2', '', 'u2', 'u1'], ['t1', 't2', 't2', '', ''], ['A', 'B', '', '', ''], [1, 2, 3, 4, 5], [-1, -1, 4, -1, -1])

Expected Output: [[], [['t1', 'A']]]

Explanation: u2's only task expires exactly at time 4, so it is already gone for the list at time 4.

Input: (['create', 'set_ttl', 'list'], ['u1', '', 'u1'], ['t1', 't1', ''], ['A', '', ''], [1, 2, 3], [-1, 2, -1])

Expected Output: [[]]

Explanation: Edge case: assigning expiration equal to the current time makes the task inactive immediately from that time onward.

Input: (['list'], ['u1'], [''], [''], [1], [-1])

Expected Output: [[]]

Explanation: Edge case: listing before any task is created returns an empty list.

Hints

  1. Because operations are already chronological, you only need to move time forward once.
  2. A min-heap can tell you which tasks should expire before the next operation, while an insertion-ordered map per user preserves listing order.

Part 3: Historical Reconstruction of a User's Task List

You are given all task creations, optional expirations, and user-time queries. Queries may come in any order. Reconstruct what each queried user's task list looked like at the requested timestamp. A task is active at time t if its creation time is less than or equal to t and either it has no expiration time or t is strictly less than its expiration time. Return tasks as [task_id, content] pairs ordered by creation time; if two tasks for the same user share the same creation time, keep the order they appeared in the input arrays.

Constraints

  • 0 <= number of tasks, expirations, queries <= 200000
  • len(task_users) == len(task_ids) == len(task_contents) == len(created_at)
  • len(exp_task_ids) == len(exp_times)
  • len(query_users) == len(query_times)
  • Each task_id is unique, and each task appears at most once in the expiration arrays
  • If a task expires at time e, it must not appear in a query at time e or later

Examples

Input: (['u1', 'u1', 'u2'], ['t1', 't2', 't3'], ['A', 'B', 'C'], [1, 4, 2], ['t1', 't3'], [5, 3], ['u1', 'u1', 'u2', 'u1'], [1, 5, 3, 4])

Expected Output: [[['t1', 'A']], [['t2', 'B']], [], [['t1', 'A'], ['t2', 'B']]]

Explanation: Queries are not sorted. At time 5, t1 has expired; at time 3, u2's task is already expired because expiration is inclusive at that timestamp.

Input: (['u1', 'u1'], ['t1', 't2'], ['A', 'B'], [2, 2], ['t1'], [2], ['u1'], [2])

Expected Output: [[['t2', 'B']]]

Explanation: Edge case: t1 is created and expires at the same timestamp, so it should not appear in the historical result at that time.

Input: ([], [], [], [], [], [], ['u1', 'u2'], [0, 10])

Expected Output: [[], []]

Explanation: Edge case: no tasks exist at all.

Input: (['u1', 'u1'], ['t2', 't1'], ['B', 'A'], [5, 5], [], [], ['u1'], [5])

Expected Output: [[['t2', 'B'], ['t1', 'A']]]

Explanation: Two tasks share the same creation time, so the original input order is preserved.

Hints

  1. Think of creation and expiration as timeline events.
  2. If you sort both events and queries by time, you can sweep once from left to right and answer all historical queries efficiently.
Last updated: May 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement a Simplified DNS Resolver - Anthropic (hard)
  • Implement a Least-Recently-Used Cache - Anthropic (medium)
  • Convert Samples into Event Intervals - Anthropic (medium)
  • Convert State Stream to Events - Anthropic (medium)
  • Build a concurrent web crawler - Anthropic (medium)