FastPrepFuture-Aware Cache Eviction

Future-Aware Cache Eviction

Benchling logoBenchling● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Process the known requests through a cache of fixed capacity. On a miss when full, evict the cached key whose next request is farthest in the future; a key never requested again is farthest. Break equal next-use ties by evicting the lexicographically larger key. Return one entry per request: the evicted key, or "-" when nothing was evicted.

Function

futureAwareEvictions(requests: String[], capacity: int) → String[]

Examples

Example 1

requests = ["a","a","b","c","b","c"]capacity = 2return = ["-","-","-","a","-","-"]

a is never needed again when c arrives, so it is evicted.

Constraints

  • 1 <= requests.length <= 200000.
  • 1 <= capacity <= 1000.
  • Keys contain lowercase letters and digits.

More Benchling problems

See Benchling hiring insights
public String[] futureAwareEvictions(String[] requests, int capacity) {
    // Write your code here.
}
requests["a","a","b","c","b","c"]
capacity2
expected["-", "-", "-", "a", "-", "-"]
Checking account…