11A system that grows in levels (the CodeSignal format)

hardpython
90:00

11 A system that grows in levels (the CodeSignal format)

Anthropic's online assessment, as candidates describe it, is one problem built up over 4 levels in 90 minutes (1 report), where later levels unlock only once earlier ones are done (1 report), and in one report each new stage forced a refactor (1 report). OpenAI candidates report in-memory database and key-value store builds too (oai-085, oai-086, prep sites only). This is practice in that shape, not a leaked question.

Set a 90-minute timer. Read one level at a time, and run only that level's tests until they pass: pytest problems/11_progressive_db -k level1. Earlier levels must keep passing.

Build InMemoryDB in solution.py. Records are key -> field -> value, all strings.

Level 1. set(key, field, value). get(key, field) returns the value or None. delete(key, field) returns True if the field existed.

Level 2. scan(key) returns ["field(value)", ...] sorted by field, [] for an unknown key. scan_by_prefix(key, prefix) does the same for fields starting with prefix.

Level 3. Timestamped versions that understand TTLs (level 3 and 4 tests only use these): set_at(key, field, value, timestamp), set_at_with_ttl(key, field, value, timestamp, ttl), get_at(key, field, timestamp), delete_at(key, field, timestamp), scan_at(key, timestamp), scan_by_prefix_at(key, prefix, timestamp). A record written at t with ttl exists for times t <= now < t + ttl. set_at without a TTL makes the record permanent. An expired record behaves as if it isn't there, so delete_at on it returns False. Calls arrive with timestamps that never go back.

Level 4. backup(timestamp) saves the database as of timestamp and returns how many keys have at least one live field (a count of keys, not of fields). restore(timestamp, timestamp_to_restore) replaces the database with the latest backup taken at or before timestamp_to_restore. A restored record keeps the TTL it had LEFT at backup time, counted from timestamp: a record that expires at 30, backed up at 15 and restored at 100, expires at 115. A permanent record stays permanent. There is always such a backup.

Graded the way OpenAI says it grades: design, code quality, performance, tests (1 source). Readable beats clever, and a refactor that keeps every level green beats a hack.

A second timed run, after this one: CodeSignal's own Industry Coding framework brief includes a public sample task in the same four-level format. Do it cold, against the same 90-minute timer.

solution.py
Run the tests to see which pass.