Design a ranked cache that evicts the lowest-rank entry with LRU tie-breaking

Quick Overview

Implement a fixed-capacity key-value cache where every entry carries a rank and a full cache evicts the lowest-ranked entry, with least recently used order breaking ties. It tests low-level design of get, put and rank updates, recency tracking, and picking data structures that keep eviction fast.

Design a ranked cache that evicts the lowest-rank entry with LRU tie-breaking

Company: LinkedIn

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a ranked cache. Each cached item has a rank (an integer score) in addition to its key and value, and the cache has a fixed `capacity`. When the cache is full and a new item must be inserted, eviction picks the item with the lowest rank. If several items share the lowest rank, the least recently used (LRU) item among them is evicted. Accessing an item with `get` or changing its rank with `update_rank` counts as a use and refreshes its recency. Inserting a new key with `put` also counts as a use. Updating an existing key with `put` refreshes both its value and its recency but does not change its rank. Implement the following methods: - `get(key: int) -> int`: return the value associated with `key`, or `-1` if it is not present. Refreshes the recency of the key. - `put(key: int, value: int, rank: int) -> None`: insert or update the key with the given value. On insertion the given rank is used; on update of an existing key the rank is left unchanged. Refreshes recency. If capacity is exceeded on insert, evict the entry with the lowest rank (ties broken by least recently used). - `update_rank(key: int, rank: int) -> None`: update the rank of an existing key and refresh its recency. If the key is not present, do nothing. Example 1: ```text Input: ["RankedCache", "put", "put", "put", "get", "put", "get", "get"] [[2], [1, 100, 5], [2, 200, 3], [3, 300, 4], [2], [4, 400, 6], [1], [3]] Output: [null, null, null, null, -1, null, 100, -1] Explanation: cache = RankedCache(2) # capacity = 2 cache.put(1, 100, 5) # cache: {1:(v=100, r=5)} cache.put(2, 200, 3) # cache: {1:(100, 5), 2:(200, 3)} cache.put(3, 300, 4) # evicts key 2 (lowest rank 3), cache: {1:(100, 5), 3:(300, 4)} cache.get(2) # -1, key 2 was evicted cache.put(4, 400, 6) # evicts key 3 (lowest rank 4), cache: {1:(100, 5), 4:(400, 6)} cache.get(1) # 100 cache.get(3) # -1, key 3 was evicted ``` Example 2: ```text Input: ["RankedCache", "put", "put", "update_rank", "put", "get", "get"] [[2], [1, 10, 1], [2, 20, 2], [1, 5], [3, 30, 3], [1], [2]] Output: [null, null, null, null, null, 10, -1] Explanation: cache = RankedCache(2) # capacity = 2 cache.put(1, 10, 1) # cache: {1:(10, r=1)} cache.put(2, 20, 2) # cache: {1:(10, 1), 2:(20, 2)} cache.update_rank(1, 5) # key 1 rank goes from 1 to 5, recency refreshed cache.put(3, 30, 3) # evicts key 2 (lowest rank 2), cache: {1:(10, 5), 3:(30, 3)} cache.get(1) # 10 cache.get(2) # -1, key 2 was evicted ``` ```hint Comparing two candidates Eviction compares entries by rank first and recency second. Recency only has to be comparable between entries, so ask what the simplest value is that orders every use. ``` ```hint When the scan is too slow A full scan at eviction time is correct. If the interviewer wants faster eviction, think about a structure ordered by the eviction key, and what has to happen to it each time an entry's rank or recency changes. ``` ### Constraints & Assumptions - `1 <= capacity <= 10⁴` - `0 <= key, value <= 10⁹` - `-10⁹ <= rank <= 10⁹` - At most `2 * 10⁴` calls will be made to `get`, `put`, and `update_rank`. ### Clarifying Questions to Ask - When a new key's rank is lower than every rank already in the cache, is the new key still stored (with an existing entry evicted), or can the new key itself be the one evicted? - Is a linear scan over the cache at eviction time acceptable, or should every operation be logarithmic or better? - Which operations dominate the workload: reads, inserts, or rank updates? - Will the cache be called from more than one thread? ### What a Strong Answer Covers - An eviction order that compares rank first and recency second, with a recency value that every use updates. - The exact update rules: `put` on an existing key changes value and recency but keeps the rank, missing keys in `get` and `update_rank` change nothing, and eviction happens only when a new key arrives at a full cache. - The cost of each operation, starting from a correct baseline and moving to a faster ordered structure when asked. - Keeping that ordered structure consistent when an entry's rank or recency changes. - Test cases for ties on rank, a rank update that saves an entry from eviction, and an update of an existing key at full capacity. ### Follow-up Questions - How would you bring every operation to $O(\log n)$, and how do you handle entries whose position changes when your heap has no decrease-key operation? - How would you make the cache safe for concurrent use, given that `get` also changes state? - How would the design change if entries also expired after a time-to-live? - What changes if the rank should come from access frequency instead of being set by the caller?

Overview: Implement a fixed-capacity key-value cache where every entry carries a rank and a full cache evicts the lowest-ranked entry, with least recently used order breaking ties. It tests low-level design of get, put and rank updates, recency tracking, and picking data structures that keep eviction fast.

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

Design a ranked cache. Each cached item has a rank (an integer score) in addition to its key and value, and the cache has a fixed capacity.

When the cache is full and a new item must be inserted, eviction picks the item with the lowest rank. If several items share the lowest rank, the least recently used (LRU) item among them is evicted.

Accessing an item with get or changing its rank with update_rank counts as a use and refreshes its recency. Inserting a new key with put also counts as a use. Updating an existing key with put refreshes both its value and its recency but does not change its rank.

Implement the following methods:

  • get(key: int) -> int : return the value associated with key , or -1 if it is not present. Refreshes the recency of the key.
  • put(key: int, value: int, rank: int) -> None : insert or update the key with the given value. On insertion the given rank is used; on update of an existing key the rank is left unchanged. Refreshes recency. If capacity is exceeded on insert, evict the entry with the lowest rank (ties broken by least recently used).
  • update_rank(key: int, rank: int) -> None : update the rank of an existing key and refresh its recency. If the key is not present, do nothing.

Example 1:

Input:
["RankedCache", "put",       "put",       "put",       "get", "put",       "get", "get"]
[[2],           [1, 100, 5], [2, 200, 3], [3, 300, 4], [2],   [4, 400, 6], [1],   [3]]

Output: [null, null, null, null, -1, null, 100, -1]

Explanation:
cache = RankedCache(2)    # capacity = 2
cache.put(1, 100, 5)      # cache: {1:(v=100, r=5)}
cache.put(2, 200, 3)      # cache: {1:(100, 5), 2:(200, 3)}
cache.put(3, 300, 4)      # evicts key 2 (lowest rank 3), cache: {1:(100, 5), 3:(300, 4)}
cache.get(2)              # -1, key 2 was evicted
cache.put(4, 400, 6)      # evicts key 3 (lowest rank 4), cache: {1:(100, 5), 4:(400, 6)}
cache.get(1)              # 100
cache.get(3)              # -1, key 3 was evicted

Example 2:

Input:
["RankedCache", "put",      "put",      "update_rank", "put",      "get", "get"]
[[2],           [1, 10, 1], [2, 20, 2], [1, 5],        [3, 30, 3], [1],   [2]]

Output: [null, null, null, null, null, 10, -1]

Explanation:
cache = RankedCache(2)    # capacity = 2
cache.put(1, 10, 1)       # cache: {1:(10, r=1)}
cache.put(2, 20, 2)       # cache: {1:(10, 1), 2:(20, 2)}
cache.update_rank(1, 5)   # key 1 rank goes from 1 to 5, recency refreshed
cache.put(3, 30, 3)       # evicts key 2 (lowest rank 2), cache: {1:(10, 5), 3:(30, 3)}
cache.get(1)              # 10
cache.get(2)              # -1, key 2 was evicted

Constraints & Assumptions

  • 1 <= capacity <= 10⁴
  • 0 <= key, value <= 10⁹
  • -10⁹ <= rank <= 10⁹
  • At most 2 * 10⁴ calls will be made to get , put , and update_rank .

Clarifying Questions to Ask Guidance

  • When a new key's rank is lower than every rank already in the cache, is the new key still stored (with an existing entry evicted), or can the new key itself be the one evicted?
  • Is a linear scan over the cache at eviction time acceptable, or should every operation be logarithmic or better?
  • Which operations dominate the workload: reads, inserts, or rank updates?
  • Will the cache be called from more than one thread?

What a Strong Answer Covers Guidance

  • An eviction order that compares rank first and recency second, with a recency value that every use updates.
  • The exact update rules: put on an existing key changes value and recency but keeps the rank, missing keys in get and update_rank change nothing, and eviction happens only when a new key arrives at a full cache.
  • The cost of each operation, starting from a correct baseline and moving to a faster ordered structure when asked.
  • Keeping that ordered structure consistent when an entry's rank or recency changes.
  • Test cases for ties on rank, a rank update that saves an entry from eviction, and an update of an existing key at full capacity.

Follow-up Questions Guidance

  • How would you bring every operation to O(log⁡n)O(\log n) , and how do you handle entries whose position changes when your heap has no decrease-key operation?
  • How would you make the cache safe for concurrent use, given that get also changes state?
  • How would the design change if entries also expired after a time-to-live?
  • What changes if the rank should come from access frequency instead of being set by the caller?