PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Meta

Implement a versioned in-memory DB with CAS and history

Last updated: Mar 29, 2026

Quick Overview

This question evaluates a candidate's ability to design and implement time-versioned in-memory data structures, conditional concurrency primitives (compare-and-set and compare-and-delete), TTL expiration semantics, and efficient historical reads.

  • medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Implement a versioned in-memory DB with CAS and history

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

## In-Memory DB V2: TTL + Compare-And-Set/Delete + Historical Reads Implement an in-memory database keyed by `(key, field)` supporting TTL, conditional updates, scans, and historical queries. ### Data model / rules - For each `(key, field)` store: - `value` - `expire_at` (integer; `+∞` means never expires) - A record is visible at time `t` iff `t < expire_at`. - All operations are given a `timestamp`. You may assume operations are provided in **non-decreasing timestamp order**. - You must also support historical queries: retrieve what value was valid at some earlier time. ### API 1. `set(timestamp, key, field, value) -> void` - Set with no TTL (`expire_at = +∞`). 2. `set_with_ttl(timestamp, key, field, value, ttl) -> void` - Set with `expire_at = timestamp + ttl`. 3. `get(timestamp, key, field) -> value | null` - Return current value if exists and not expired at `timestamp`, else `null`. 4. `compare_and_set(timestamp, key, field, expected_value, new_value) -> bool` - If `(key, field)` exists and is not expired at `timestamp` and its current value equals `expected_value`, update it to `new_value` (`expire_at = +∞`) and return `true`. - Otherwise return `false`. 5. `compare_and_set_with_ttl(timestamp, key, field, expected_value, new_value, ttl) -> bool` - Same as above, but sets `expire_at = timestamp + ttl`. 6. `compare_and_delete(timestamp, key, field, expected_value) -> bool` - If current visible value equals `expected_value`, delete it and return `true`, else return `false`. 7. `scan(timestamp, key) -> list<string>` - Return all non-expired fields for `key` at `timestamp`. - Sort by `field` ascending. - Format each item as `"field(value)"`. 8. `scan_by_prefix(timestamp, key, prefix) -> list<string>` - Like `scan`, but only fields starting with `prefix`. 9. `get_value_at(timestamp, key, field, at_timestamp) -> value | null` - Return the value that was valid for `(key, field)` at time `at_timestamp`. - Return `null` if it did not exist, was deleted, or was expired at `at_timestamp`. ### Notes - You will likely need to keep a per-(key,field) history of changes to answer `get_value_at` efficiently. - Assume up to ~1e5 operations.

Quick Answer: This question evaluates a candidate's ability to design and implement time-versioned in-memory data structures, conditional concurrency primitives (compare-and-set and compare-and-delete), TTL expiration semantics, and efficient historical reads.

Related Interview Questions

  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)
  • Solve a Key-Door Corridor Maze - Meta (medium)
  • Solve Array Merge and Parentheses Cleanup - Meta (medium)
Meta logo
Meta
Feb 11, 2026, 12:00 AM
Software Engineer
Take-home Project
Coding & Algorithms
5
0

In-Memory DB V2: TTL + Compare-And-Set/Delete + Historical Reads

Implement an in-memory database keyed by (key, field) supporting TTL, conditional updates, scans, and historical queries.

Data model / rules

  • For each (key, field) store:
    • value
    • expire_at (integer; +∞ means never expires)
  • A record is visible at time t iff t < expire_at .
  • All operations are given a timestamp . You may assume operations are provided in non-decreasing timestamp order .
  • You must also support historical queries: retrieve what value was valid at some earlier time.

API

  1. set(timestamp, key, field, value) -> void
    • Set with no TTL ( expire_at = +∞ ).
  2. set_with_ttl(timestamp, key, field, value, ttl) -> void
    • Set with expire_at = timestamp + ttl .
  3. get(timestamp, key, field) -> value | null
    • Return current value if exists and not expired at timestamp , else null .
  4. compare_and_set(timestamp, key, field, expected_value, new_value) -> bool
    • If (key, field) exists and is not expired at timestamp and its current value equals expected_value , update it to new_value ( expire_at = +∞ ) and return true .
    • Otherwise return false .
  5. compare_and_set_with_ttl(timestamp, key, field, expected_value, new_value, ttl) -> bool
    • Same as above, but sets expire_at = timestamp + ttl .
  6. compare_and_delete(timestamp, key, field, expected_value) -> bool
    • If current visible value equals expected_value , delete it and return true , else return false .
  7. scan(timestamp, key) -> list<string>
    • Return all non-expired fields for key at timestamp .
    • Sort by field ascending.
    • Format each item as "field(value)" .
  8. scan_by_prefix(timestamp, key, prefix) -> list<string>
    • Like scan , but only fields starting with prefix .
  9. get_value_at(timestamp, key, field, at_timestamp) -> value | null
    • Return the value that was valid for (key, field) at time at_timestamp .
    • Return null if it did not exist, was deleted, or was expired at at_timestamp .

Notes

  • You will likely need to keep a per-(key,field) history of changes to answer get_value_at efficiently.
  • Assume up to ~1e5 operations.

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Meta•More Software Engineer•Meta Software Engineer•Meta Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,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.