PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Ziprecruiter

Implement an in-memory storage with TTL and scans

Last updated: Mar 29, 2026

Quick Overview

This question evaluates implementation of an in-memory two-level key->field->value store with TTL semantics, lexicographic scans and prefix filtering, and time-travel reads, covering competencies in data structures, time-based expiry handling, and maintaining correct state across monotonic timestamps.

  • medium
  • Ziprecruiter
  • Coding & Algorithms
  • Software Engineer

Implement an in-memory storage with TTL and scans

Company: Ziprecruiter

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

## Problem Implement an in-memory storage system similar to a tiny key–field–value database. - Data model: a **2-level map** `key -> field -> value`. - Each operation includes a **timestamp** `ts` (non-decreasing across the input). - A `(key, field)` entry may optionally have a **TTL** (time-to-live), after which it is considered **expired** and must behave as if it does not exist. You will be given a sequence of queries/commands. Process them in order and return outputs for the commands that produce an output. ## Commands Assume all `key`, `field`, `value`, `prefix` are non-empty strings without spaces; `ts` and `ttl` are integers. ### Basic operations (Level 1) 1. `SET ts key field value` - Set `value` for `(key, field)` at time `ts`. - This write has **no TTL** (never expires) and overwrites any previous value/TTL. 2. `GET ts key field` - Return the current value of `(key, field)` at time `ts`. - If missing or expired, return an empty string `""`. 3. `COMPARE_AND_SET ts key field expected newValue` - If the current (non-expired) value equals `expected`, set it to `newValue` (no TTL) and return `"true"`. - Otherwise return `"false"`. 4. `COMPARE_AND_DELETE ts key field expected` - If the current (non-expired) value equals `expected`, delete the field and return `"true"`. - Otherwise return `"false"`. ### Scans (Level 2) 5. `SCAN ts key` - Return all non-expired fields for `key`, **sorted lexicographically by field name**. - Format each as `field(value)` and join with commas, e.g. `a(1),b(2)`. - If `key` has no non-expired fields, return `""`. 6. `SCAN_BY_PREFIX ts key prefix` - Same as `SCAN`, but include only fields whose name starts with `prefix`. ### TTL support (Level 3) 7. `SET_WITH_TTL ts key field value ttl` - Set `value` for `(key, field)` at time `ts` with expiration time `expireAt = ts + ttl`. - The entry is considered valid for timestamps `t` such that `ts <= t < expireAt`. - Overwrites any previous value/TTL. **TTL rule:** At timestamp `ts`, an entry is visible only if it exists and is not expired at `ts`. ### Time-travel queries (Level 4 / advanced) 8. `GET_AT ts key field atTs` - Return the value that `(key, field)` had at time `atTs` (with TTL and deletes respected). - If it did not exist or was expired at `atTs`, return `""`. - You may assume `atTs <= ts`. ## Input / Output - Input: `queries`, a list where each element is a list of strings describing one command (tokens in the order shown above). - Output: a list of strings containing the results of each command that produces output: - `GET`, `COMPARE_AND_SET`, `COMPARE_AND_DELETE`, `SCAN`, `SCAN_BY_PREFIX`, `GET_AT`. - `SET` and `SET_WITH_TTL` produce no output. ## Constraints (typical interview scale) - `1 <= len(queries) <= 100000` - Timestamps are non-decreasing. - Total length of all strings is manageable in memory. Implement the function that processes the queries and returns the output list.

Quick Answer: This question evaluates implementation of an in-memory two-level key->field->value store with TTL semantics, lexicographic scans and prefix filtering, and time-travel reads, covering competencies in data structures, time-based expiry handling, and maintaining correct state across monotonic timestamps.

Related Interview Questions

  • Find Neighboring Records by Identifier - Ziprecruiter (easy)
  • Design a voting counter for restaurants - Ziprecruiter (medium)
Ziprecruiter logo
Ziprecruiter
Feb 12, 2026, 12:00 AM
Software Engineer
Take-home Project
Coding & Algorithms
2
0
Loading...

Problem

Implement an in-memory storage system similar to a tiny key–field–value database.

  • Data model: a 2-level map key -> field -> value .
  • Each operation includes a timestamp ts (non-decreasing across the input).
  • A (key, field) entry may optionally have a TTL (time-to-live), after which it is considered expired and must behave as if it does not exist.

You will be given a sequence of queries/commands. Process them in order and return outputs for the commands that produce an output.

Commands

Assume all key, field, value, prefix are non-empty strings without spaces; ts and ttl are integers.

Basic operations (Level 1)

  1. SET ts key field value
    • Set value for (key, field) at time ts .
    • This write has no TTL (never expires) and overwrites any previous value/TTL.
  2. GET ts key field
    • Return the current value of (key, field) at time ts .
    • If missing or expired, return an empty string "" .
  3. COMPARE_AND_SET ts key field expected newValue
    • If the current (non-expired) value equals expected , set it to newValue (no TTL) and return "true" .
    • Otherwise return "false" .
  4. COMPARE_AND_DELETE ts key field expected
    • If the current (non-expired) value equals expected , delete the field and return "true" .
    • Otherwise return "false" .

Scans (Level 2)

  1. SCAN ts key
    • Return all non-expired fields for key , sorted lexicographically by field name .
    • Format each as field(value) and join with commas, e.g. a(1),b(2) .
    • If key has no non-expired fields, return "" .
  2. SCAN_BY_PREFIX ts key prefix
    • Same as SCAN , but include only fields whose name starts with prefix .

TTL support (Level 3)

  1. SET_WITH_TTL ts key field value ttl
    • Set value for (key, field) at time ts with expiration time expireAt = ts + ttl .
    • The entry is considered valid for timestamps t such that ts <= t < expireAt .
    • Overwrites any previous value/TTL.

TTL rule: At timestamp ts, an entry is visible only if it exists and is not expired at ts.

Time-travel queries (Level 4 / advanced)

  1. GET_AT ts key field atTs
    • Return the value that (key, field) had at time atTs (with TTL and deletes respected).
    • If it did not exist or was expired at atTs , return "" .
    • You may assume atTs <= ts .

Input / Output

  • Input: queries , a list where each element is a list of strings describing one command (tokens in the order shown above).
  • Output: a list of strings containing the results of each command that produces output:
    • GET , COMPARE_AND_SET , COMPARE_AND_DELETE , SCAN , SCAN_BY_PREFIX , GET_AT .
    • SET and SET_WITH_TTL produce no output.

Constraints (typical interview scale)

  • 1 <= len(queries) <= 100000
  • Timestamps are non-decreasing.
  • Total length of all strings is manageable in memory.

Implement the function that processes the queries and returns the output list.

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Ziprecruiter•More Software Engineer•Ziprecruiter Software Engineer•Ziprecruiter 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.