Problem statement
Write function solution(items, actions). 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 allows over-reservation, accepts invalid quantities and operation types, and mutates inputs. Repair it; the function body may be rewritten. items is an array of {sku:string,stock:nonnegative integer,reserved:nonnegative integer} with unique SKUs and initial reserved<=stock. actions is an array of {type:string,sku:string,quantity:number}. Process actions in input order. Reject unknown SKUs, non-positive or non-integer quantities, and unknown types. receive increases stock by quantity. reserve increases reserved only if stock-reserved>=quantity. release decreases reserved only if reserved>=quantity. Rejected actions make no changes. SKUs are case-sensitive. Return {items:[{sku,stock,reserved,available:stock-reserved}],accepted:[boolean per action]}, preserving original item order and action order. Do not mutate inputs. 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
[
[
{
"sku": "a",
"stock": 5,
"reserved": 0
}
],
[
{
"type": "reserve",
"sku": "a",
"quantity": 3
}
]
]Return value
{
"items": [
{
"sku": "a",
"stock": 5,
"reserved": 3,
"available": 2
}
],
"accepted": [
true
]
}Example 2
Function arguments
[
[
{
"sku": "a",
"stock": 2,
"reserved": 0
}
],
[
{
"type": "reserve",
"sku": "a",
"quantity": 3
}
]
]Return value
{
"items": [
{
"sku": "a",
"stock": 2,
"reserved": 0,
"available": 2
}
],
"accepted": [
false
]
}Example 3
Function arguments
[
[
{
"sku": "a",
"stock": 3,
"reserved": 0
}
],
[
{
"type": "reserve",
"sku": "a",
"quantity": 3
},
{
"type": "release",
"sku": "a",
"quantity": 3
},
{
"type": "reserve",
"sku": "a",
"quantity": 3
}
]
]Return value
{
"items": [
{
"sku": "a",
"stock": 3,
"reserved": 3,
"available": 0
}
],
"accepted": [
true,
true,
true
]
}