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. capacity is an integer from 0 to 1024. operations contains {type:"put",key:string,value:integer} or {type:"get",key:string}. Start with an empty cache. put stores or updates a value and makes it most recently used; if capacity is exceeded evict the least recently used item. get returns a stored value and refreshes its recency, or returns null without changing recency if missing. Return only get results as an array. With capacity=0 store nothing. Keys are case-sensitive. 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
},
{
"type": "put",
"key": "b",
"value": 2
},
{
"type": "get",
"key": "a"
},
{
"type": "put",
"key": "c",
"value": 3
},
{
"type": "get",
"key": "b"
},
{
"type": "get",
"key": "a"
}
]
]Return value
[ 1, null, 1 ]
Example 2
Function arguments
[
0,
[
{
"type": "put",
"key": "a",
"value": 1
},
{
"type": "get",
"key": "a"
}
]
]Return value
[ null ]
Example 3
Function arguments
[
1,
[
{
"type": "put",
"key": "a",
"value": 1
},
{
"type": "get",
"key": "a"
},
{
"type": "get",
"key": "a"
},
{
"type": "put",
"key": "a",
"value": 2
},
{
"type": "get",
"key": "a"
}
]
]Return value
[ 1, 1, 2 ]