Implement an in-memory DB with TTL backup/restore
Company: Anthropic
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
This question evaluates implementation and reasoning skills for an in-memory nested key→field→value store with TTL-based expirations, snapshot backup/restore semantics, and timestamped operations, testing competencies in data structures, state management, and time-aware logic within the Coding & Algorithms domain.
Company: Anthropic
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Loading coding console...
Quick Answer: This question evaluates implementation and reasoning skills for an in-memory nested key→field→value store with TTL-based expirations, snapshot backup/restore semantics, and timestamped operations, testing competencies in data structures, state management, and time-aware logic within the Coding & Algorithms domain.
Input: ["SET 1 user name alice", "GET 1 user name", "SET_TTL 2 user age 30 3", "GET 4 user age", "GET 5 user age", "SCAN 5 user"]
Expected Output: ["alice", "30", "", "name(alice)"]
Explanation: The age field is valid at time 4 but expired at time 5, so only name remains in the scan.
Input: ["SET 1 app aa 1", "SET 1 app ab 2", "SET 1 app b 3", "SCAN_PREFIX 1 app a", "DELETE 2 app aa", "DELETE 2 app aa", "SCAN 2 app"]
Expected Output: ["aa(1),ab(2)", "true", "false", "ab(2),b(3)"]
Explanation: Prefix scan returns the two a* fields in lexicographic order. Deleting aa succeeds once and then fails because it is already gone.
Input: ["SET 1 a x v1", "SET_TTL 2 a y v2 5", "BACKUP 4", "SET 5 a x changed", "GET 6 a y", "RESTORE 8 4", "GET 9 a x", "GET 10 a y", "GET 11 a y"]
Expected Output: ["1", "v2", "true", "v1", "v2", ""]
Explanation: At backup time 4, y has 3 units of TTL remaining. After restoring at time 8, y expires at time 11 and x returns to v1.
Input: ["RESTORE 1 0", "SET 2 k f v", "BACKUP 3", "SET 4 k g w", "BACKUP 5", "DELETE 6 k f", "RESTORE 7 4", "SCAN 7 k", "RESTORE 8 100", "SCAN 8 k"]
Expected Output: ["false", "1", "1", "true", "true", "f(v)", "true", "f(v),g(w)"]
Explanation: The first restore fails because no backup exists yet. Restoring with at_timestamp=4 picks the backup at time 3, while restoring with at_timestamp=100 picks the latest backup at time 5.
Input: []
Expected Output: []
Explanation: No queries means no outputs.