KatInterview

Tiered Cache with TTL and Priority Fallback

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designhash-tableheapdata-structure-design

Last updated

Implement a hierarchical key-value cache with expiration times, priority-based lookups, and lazy eviction across multiple storage tiers.

Problem Description

Build a cache system that organizes data into multiple priority tiers. Each tier stores key-value pairs that expire after a designated time. When retrieving data, the system searches tiers in priority order and returns the first valid (non-expired) match.

Implement a function that processes a sequence of operations on this tiered cache structure.

Operations

Your function receives three parameters:

  • num_tiers: The number of priority tiers (indexed 0 to num_tiers - 1, where tier 0 has highest priority)
  • operations: A list of operation names
  • arguments: A list of argument lists, one per operation

The operations are:

  1. "store": [key, value, tier, expiry_time]

    • Place an item in the specified tier with an absolute expiration timestamp
    • If the key already exists in that tier, replace both its value and expiration time
  2. "retrieve": [key, current_time]

    • Search for the key starting from tier 0 upward
    • Return the value from the lowest-indexed tier where the key exists and has not expired (expiry_time > current_time)
    • Remove any expired entries encountered during the search
    • Return None if the key is not found or all copies have expired
  3. "purge_expired": [current_time]

    • Remove all entries across all tiers where expiry_time <= current_time
    • Return the count of items removed

Return a list of results: None for "store" operations, the retrieved value or None for "retrieve", and the removal count for "purge_expired".

Constraints

  • 1 <= num_tiers <= 16
  • 0 <= tier < num_tiers
  • 1 <= len(key) <= 64 (alphanumeric characters)
  • 0 <= current_time, expiry_time <= 2 × 10^9
  • -2^31 <= value <= 2^31 - 1
  • Total operations ≤ 10^5

Notes

  • An item is considered expired when expiry_time <= current_time (the boundary case counts as expired)
  • The same key can exist in multiple tiers with different values and expiration times
  • Lazy eviction during retrieve means those items won't be counted again by purge_expired

Examples

Example 1

Input: tiered_cache_operations(3, ['store', 'store', 'store', 'retrieve', 'retrieve', 'retrieve', 'purge_expired'], [['bid', 100, 1, 50], ['bid', 105, 0, 30], ['ask', 110, 2, 40], ['bid', 20], ['bid', 35], ['ask', 45], [60]])

Output: [None, None, None, 105, 100, None, 1]

Example 2

Input: tiered_cache_operations(2, ['store', 'store', 'retrieve', 'retrieve'], [['x', -4, 0, 10], ['x', 7, 1, 20], ['x', 10], ['x', 20]])

Output: [None, None, 7, None]

Hints

Hint 1

Each tier can be represented as a hash map storing key to (value, expiration_time) pairs for O(1) lookup

Hint 2

The retrieve operation should check tiers in order from 0 upward and delete expired entries as it encounters them

Hint 3

For purge_expired, iterate through all keys in each tier and collect those that have expired - this is simpler than maintaining a separate data structure