Problem · Design

File-Content LRU Cache

Learn this problem
MediumAmazon logoAmazonFULLTIMEONSITE INTERVIEW
See Amazon hiring insights

Problem statement

Simulate a least-recently-used cache that stores file contents. The cache holds at most capacity files.

Each operation is either PUT|path|content or GET|path. A PUT inserts or replaces the complete content. A successful GET returns the content and makes that path most recently used; a missing path returns the empty string. When a new path exceeds capacity, evict the least recently used path.

Return all GET results in operation order.

Function

runFileCache(capacity: int, operations: String[]) → String[]

Examples

Example 1

capacity = 2operations = ["PUT|/a|alpha","PUT|/b|beta","GET|/a","PUT|/c|gamma","GET|/b","GET|/c"]return = ["alpha","","gamma"]

Reading /a refreshes it, so /b is evicted by the insertion of /c.

Example 2

capacity = 1operations = ["PUT|/x|old","PUT|/x|new","GET|/x"]return = ["new"]

Replacing one path updates its content without increasing cache size.

Constraints

  • 0 <= capacity <= 50000
  • 1 <= operations.length <= 100000
  • Paths and contents are non-empty printable strings and do not contain |.
  • The empty string is reserved for a cache miss.

More Amazon problems

drafts saved locally
public String[] runFileCache(int capacity, String[] operations) {
    // Write your code here.
}
capacity2
operations["PUT|/a|alpha","PUT|/b|beta","GET|/a","PUT|/c|gamma","GET|/b","GET|/c"]
expected["alpha", "", "gamma"]
checking account