Reported May 2026
Anthropicdesign

In-Memory Database with Backup and Restore

Reported by candidates from Anthropic's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Anthropic OA. Under 2s to a working solution.
Founder's read

You're building an in-memory key-value database with snapshot backup and restore at Anthropic (May 2026). This is a design problem disguised as a systems question. You need SET, GET, DELETE, BACKUP, and RESTORE commands. The trap: naive deep-copy on every backup will timeout at scale. The pattern is structural sharing or copy-on-write versioning. StealthCoder will catch you if you blank on the architecture mid-OA.

The problem

Complete the function below. The function receives the full standard input as a single string and must return the exact standard output lines for the described problem. Original prompt Problem: In-Memory Database with Backup and Restore Build a toy core-logic, purely in-memory key-value database (no UI/framework), using only the standard library. You must process input commands and output the result for each command. Data Model The database is a mapping key -> value, both strings. All data is in memory only. Commands Implement at least the following commands (the interview note says BACKUP and RESTORE were tested): SET key value Set key to value (overwrite allowed). Output: "OK" GET key If key exists output its value, else output "NULL". DELETE key Delete key. If deleted output "OK", else output "NULL". BACKUP Create a snapshot of the current database state. Output: an integer backup_id (starting from 1, increasing). RESTORE backup_id Restore database state to the snapshot identified by backup_id. If backup_id exists: restore and output "OK"; otherwise output "NULL". Input Format First line: integer n, number of commands. Next n lines: one command per line. Output Format Output one line per command, the command result. Constraints 1 <= n <= 2 * 10^5 key, value are non-empty strings with no spaces. Implement efficiently: BACKUP/RESTORE should not deep-copy the entire map on each backup (consider structural sharing, incremental snapshots, copy-on-write, or versioning). Sample Tests Test 1 Input: 7 SET a 1 BACKUP SET a 2 GET a RESTORE 1 GET a DELETE a Output: OK 1 OK 2 OK 1 OK Test 2 Input: 5 GET missing RESTORE 99 SET k v BACKUP RESTORE 1 Output: NULL NULL OK 1 OK Test 3 Input: 8 SET x 10 SET y 20 BACKUP DELETE x GET x RESTORE 1 GET x GET y Output: OK OK 1 OK NULL OK 10 20 Test 4 Input: 6 SET a 1 BACKUP SET b 2 BACKUP RESTORE 1 GET b Output: OK 1 OK 2 OK NULL Test 5 Input: 6 SET a 1 BACKUP SET a 3 BACKUP RESTORE 2 GET a Output: OK 1 OK 2 OK 3 Function Description Complete solveInMemoryDatabaseBackupRestore. It has one parameter, String input, containing the full stdin payload. Return the stdout payload as an array of lines, without trailing newline characters. The returned string must match the expected standard output for the sample input. Use the limits and requirements stated in the prompt.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The core trick is that BACKUP shouldn't clone the entire map each time. Instead, store snapshots as references to immutable state or use copy-on-write: each backup is a pointer to the current map, and only when SET modifies data after a backup do you diverge. A simpler, correct approach: maintain a list of map versions. On BACKUP, add the current state (shallow reference or full copy, depending on constraints). On RESTORE, swap the active map. On SET after a restore, copy the current state if it's a backed-up version, then modify. Test cases show RESTORE must rewind all writes, not just restore keys. The problem is less about algorithmic cleverness and more about clean state management and avoiding unnecessary allocations.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill In-Memory Database with Backup and Restore cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Anthropic's OA.

Anthropic reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

In-Memory Database with Backup and Restore FAQ

Do I really need copy-on-write or structural sharing for this OA?+

Not necessarily. With n up to 2*10^5 commands, a straightforward versioning approach (list of map snapshots) works fine. Deep-copy on BACKUP is the risky path. Use a version list and mutable operations on the active map instead.

What happens if you RESTORE to a backup, then SET, then RESTORE again?+

The second RESTORE resets to the exact state at that backup ID, discarding any changes made after the restore. Test 4 confirms: restore to backup 1, set b=2, then restore 1 again should show b missing.

Should I store full copies or references in my backup list?+

References work if the map is immutable after backup. If you modify in-place, you'll corrupt old snapshots. Safest: on BACKUP, deep-copy the current map into the backup list. On RESTORE, swap or copy from the backup.

How do I handle invalid backup IDs?+

Return "NULL". Backup IDs start at 1 and increment sequentially. If the ID is out of range or doesn't exist, the RESTORE fails. Test 2 shows RESTORE 99 on a database with only 1 backup returns NULL.

Can I precompute or optimize this in 24 hours before the OA?+

Understand the test cases by hand first. Code a simple versioning approach: list of maps, current index. Implement SET, GET, DELETE on the active map. BACKUP appends a copy. RESTORE swaps the active index. This passes all samples.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Anthropic.

OA at Anthropic?
Invisible during screen share
Get it