Problem statement
Write function solution(capacity, operations). Inputs and outputs are JSON values. Do not modify input arrays or objects. External libraries, network access, and asynchronous functions are not supported. The starter mishandles expiration, replacement, recency, and deletion. Repair it into an expiring LRU cache; you may rewrite the function body. capacity is an integer from 0 through 1024. operations contains put {type:"put",key:string,value:integer,timestamp:nonnegative integer,ttlMs:nonnegative integer}, get {type:"get",key:string,timestamp}, or delete {type:"delete",key:string,timestamp}. Times are milliseconds and timestamps are nondecreasing. Keys are case-sensitive. Before each operation remove all entries with expiration<=timestamp. put replaces the value, sets expiration=timestamp+ttlMs, and makes it most recently used. ttlMs=0 removes the existing key without storing a value. capacity=0 stores nothing. Over capacity, evict the least recently used unexpired entry. get returns a valid value and updates recency without extending expiration; a miss returns null without changing recency. delete removes the key; missing keys are ignored. An old expiration must not delete a value replaced by a newer put. Process equal timestamps in input order. Return only get results in order. Input limits: at most 50,000 items per input array and 1,000 characters per string. Numbers are finite; integers and accumulated integer results stay in the JavaScript safe-integer range. Other types and validity rules follow the statement above.
Example input and output
Example 1
Function arguments
[
2,
[
{
"type": "put",
"key": "a",
"value": 1,
"timestamp": 0,
"ttlMs": 10
},
{
"type": "get",
"key": "a",
"timestamp": 9
},
{
"type": "get",
"key": "a",
"timestamp": 10
}
]
]Return value
[ 1, null ]
Example 2
Function arguments
[
2,
[
{
"type": "put",
"key": "a",
"value": 1,
"timestamp": 0,
"ttlMs": 5
},
{
"type": "put",
"key": "a",
"value": 2,
"timestamp": 2,
"ttlMs": 10
},
{
"type": "get",
"key": "a",
"timestamp": 5
},
{
"type": "get",
"key": "a",
"timestamp": 12
}
]
]Return value
[ 2, null ]
Example 3
Function arguments
[
1,
[
{
"type": "put",
"key": "a",
"value": 1,
"timestamp": 0,
"ttlMs": 2
},
{
"type": "delete",
"key": "a",
"timestamp": 1
},
{
"type": "put",
"key": "a",
"value": 2,
"timestamp": 1,
"ttlMs": 10
},
{
"type": "get",
"key": "a",
"timestamp": 2
}
]
]Return value
[ 2 ]