Design a two-tier key-value store with an LRU hot tier, TTL expiry and an unbounded cold tier

Quick Overview

Design a two-tier key-value store with a bounded hot tier whose entries expire after a fixed TTL and an unbounded cold tier that keeps demoted values. It tests LRU eviction, exact promotion and demotion rules, and keeping the per-call cost of expiration low.

Design a two-tier key-value store with an LRU hot tier, TTL expiry and an unbounded cold tier

Company: Netapp

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a two-tier key-value store. Tier B1 is a hot tier with a fixed positive `capacity`. Every B1 entry has an expiry time and a recency rank. Tier B2 is an unbounded cold tier. A value is never lost when its key moves between the tiers. B2 entries do not expire and leave B2 only when promoted to B1. Every key is unique across both tiers, and values are nonnegative integers. Every call passes a timestamp `now`, and timestamps never decrease. Before every operation, each B1 entry whose expiry `<= now` moves to B2 and keeps its value. Writing a key, or reading a key that exists in either tier, promotes it to B1, sets its expiry to `now + ttl` and marks it most recently used. When a key must be promoted into a full B1, the least recently used B1 key is demoted to B2 first. Refreshing a key that is already in B1 never evicts another key. After processing expirations, a miss makes no further changes. The inspection call (`tierOf`) only runs expiration. It never refreshes TTL, changes recency or promotes. When several calls share a timestamp, their order decides recency. Implement the following methods: - `TwoTierTTLStore(capacity: int, ttl: int)` initializes an empty store whose B1 tier holds at most `capacity` entries and whose B1 entries live for `ttl` time units after each touch. - `put(key: str, value: int, now: int) -> None` expires due B1 entries, then inserts or updates the key with the value and promotes it to B1 (demoting the LRU B1 key first if B1 is full and the key is not already in B1), sets its expiry to `now + ttl` and marks it most recently used. - `get(key: str, now: int) -> int` expires due B1 entries, then returns the key's value from either tier, promoting it to B1, refreshing its expiry to `now + ttl` and marking it most recently used. On a miss it returns `-1` and makes no further changes after expiration. - `tierOf(key: str, now: int) -> int` expires due B1 entries, then returns `1` if the key is in B1, `2` if it is in B2, or `0` if it is absent, without refreshing TTL, changing recency or promoting. Example 1: ```text Calls: ["TwoTierTTLStore", "put", "put", "get", "put", "tierOf", "get", "tierOf"] Args: [[2, 10], ["a", 1, 0], ["b", 2, 1], ["a", 2], ["c", 3, 3], ["b", 4], ["b", 5], ["a", 5]] Output: [null, null, null, 1, null, 2, 2, 2] ``` Reading `a` at time 2 makes it the most recently used key, so the `put` of `c` at time 3 finds B1 full and demotes `b`, the LRU key, to B2. Reading `b` at time 5 promotes it back into the full B1, which first demotes `a`, now the LRU key. No entry expires in this run: the earliest expiry ever set is 10 (`a` before its refresh at time 2), and the last call is at time 5. Example 2: ```text Calls: ["TwoTierTTLStore", "put", "put", "put", "tierOf", "tierOf", "get", "get", "tierOf"] Args: [[2, 5], ["x", 7, 0], ["y", 8, 2], ["y", 9, 3], ["x", 4], ["x", 5], ["z", 6], ["y", 8], ["y", 8]] Output: [null, null, null, null, 1, 2, -1, 9, 1] ``` Updating `y` at time 3 refreshes it inside a full B1 without evicting `x`. `x` expires at 5, so `tierOf("x", 4)` still sees it in B1 and `tierOf("x", 5)` finds it in B2. `get("z", 6)` is a miss. At time 8 `y` expires and moves to B2 with value 9, and the same `get` call then promotes it back to B1 with a new expiry of 13. ```hint Two orders Before reaching for a priority queue of expiry times, look at how an entry's expiry relates to its last touch when every entry uses the same `ttl` and time never goes backwards. ``` ```hint Cost of expiration Expiration runs before every call. Make sure no call scans all of B1, and count how many times a single entry can be expired between two promotions. ``` ### Constraints & Assumptions - `1 <= capacity <= 10^5` - `1 <= ttl <= 10^5` - `0 <= now <= 10^9`, and `now` never decreases across calls - `0 <= value <= 10^9` - Keys are nonempty lowercase ASCII strings with `1 <= key.length <= 30` - At most `10^5` calls in total are made to `put`, `get` and `tierOf`. ### What a Strong Answer Covers - Data structures for B1 and B2 that give constant-time lookup, recency updates, LRU eviction and expiration. - Why expiration can be processed without scanning every B1 entry, and the amortized cost it adds per call. - Exact handling of each transition: a refresh inside B1 with no eviction, a promotion of a B2 or new key into a full B1, demotion on expiry, and a miss that changes nothing after expiration. - A `tierOf` that runs expiration only and leaves recency and expiry untouched. - Time and space complexity of every operation. ### Follow-up Questions - How would the design change if each `put` could pass its own TTL? - How would you bound B2 as well, and which entries would you drop from it? - How would you make the store safe for concurrent callers, given that even `tierOf` can change state? - What breaks if timestamps can arrive slightly out of order?

Overview: Design a two-tier key-value store with a bounded hot tier whose entries expire after a fixed TTL and an unbounded cold tier that keeps demoted values. It tests LRU eviction, exact promotion and demotion rules, and keeping the per-call cost of expiration low.

Netapp logo
Netapp
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a two-tier key-value store. Tier B1 is a hot tier with a fixed positive capacity. Every B1 entry has an expiry time and a recency rank. Tier B2 is an unbounded cold tier. A value is never lost when its key moves between the tiers. B2 entries do not expire and leave B2 only when promoted to B1. Every key is unique across both tiers, and values are nonnegative integers.

Every call passes a timestamp now, and timestamps never decrease. Before every operation, each B1 entry whose expiry <= now moves to B2 and keeps its value. Writing a key, or reading a key that exists in either tier, promotes it to B1, sets its expiry to now + ttl and marks it most recently used. When a key must be promoted into a full B1, the least recently used B1 key is demoted to B2 first. Refreshing a key that is already in B1 never evicts another key. After processing expirations, a miss makes no further changes. The inspection call (tierOf) only runs expiration. It never refreshes TTL, changes recency or promotes. When several calls share a timestamp, their order decides recency.

Implement the following methods:

  • TwoTierTTLStore(capacity: int, ttl: int) initializes an empty store whose B1 tier holds at most capacity entries and whose B1 entries live for ttl time units after each touch.
  • put(key: str, value: int, now: int) -> None expires due B1 entries, then inserts or updates the key with the value and promotes it to B1 (demoting the LRU B1 key first if B1 is full and the key is not already in B1), sets its expiry to now + ttl and marks it most recently used.
  • get(key: str, now: int) -> int expires due B1 entries, then returns the key's value from either tier, promoting it to B1, refreshing its expiry to now + ttl and marking it most recently used. On a miss it returns -1 and makes no further changes after expiration.
  • tierOf(key: str, now: int) -> int expires due B1 entries, then returns 1 if the key is in B1, 2 if it is in B2, or 0 if it is absent, without refreshing TTL, changing recency or promoting.

Example 1:

Calls:  ["TwoTierTTLStore", "put",       "put",       "get",    "put",       "tierOf", "get",    "tierOf"]
Args:   [[2, 10],           ["a", 1, 0], ["b", 2, 1], ["a", 2], ["c", 3, 3], ["b", 4], ["b", 5], ["a", 5]]
Output: [null,              null,        null,        1,        null,        2,        2,        2]

Reading a at time 2 makes it the most recently used key, so the put of c at time 3 finds B1 full and demotes b, the LRU key, to B2. Reading b at time 5 promotes it back into the full B1, which first demotes a, now the LRU key. No entry expires in this run: the earliest expiry ever set is 10 (a before its refresh at time 2), and the last call is at time 5.

Example 2:

Calls:  ["TwoTierTTLStore", "put",       "put",       "put",       "tierOf", "tierOf", "get",    "get",    "tierOf"]
Args:   [[2, 5],            ["x", 7, 0], ["y", 8, 2], ["y", 9, 3], ["x", 4], ["x", 5], ["z", 6], ["y", 8], ["y", 8]]
Output: [null,              null,        null,        null,        1,        2,        -1,       9,        1]

Updating y at time 3 refreshes it inside a full B1 without evicting x. x expires at 5, so tierOf("x", 4) still sees it in B1 and tierOf("x", 5) finds it in B2. get("z", 6) is a miss. At time 8 y expires and moves to B2 with value 9, and the same get call then promotes it back to B1 with a new expiry of 13.

Constraints & Assumptions

  • 1 <= capacity <= 10^5
  • 1 <= ttl <= 10^5
  • 0 <= now <= 10^9 , and now never decreases across calls
  • 0 <= value <= 10^9
  • Keys are nonempty lowercase ASCII strings with 1 <= key.length <= 30
  • At most 10^5 calls in total are made to put , get and tierOf .

What a Strong Answer Covers Guidance

  • Data structures for B1 and B2 that give constant-time lookup, recency updates, LRU eviction and expiration.
  • Why expiration can be processed without scanning every B1 entry, and the amortized cost it adds per call.
  • Exact handling of each transition: a refresh inside B1 with no eviction, a promotion of a B2 or new key into a full B1, demotion on expiry, and a miss that changes nothing after expiration.
  • A tierOf that runs expiration only and leaves recency and expiry untouched.
  • Time and space complexity of every operation.

Follow-up Questions Guidance

  • How would the design change if each put could pass its own TTL?
  • How would you bound B2 as well, and which entries would you drop from it?
  • How would you make the store safe for concurrent callers, given that even tierOf can change state?
  • What breaks if timestamps can arrive slightly out of order?