Problem statement
Write function solution(events, width). Inputs and outputs are JSON values. Do not modify input arrays or objects. External libraries, network access, and asynchronous functions are not supported. events:{timestamp:integer,service:string,durationMs:nonnegative integer,status:integer}[], width:positive integer. Bucket start=floor(timestamp/width)*width, including negative timestamps. Return {start,service,count,errors,totalMs} per bucket/service; errors counts status>=500. Sort by start, then case-sensitive JavaScript service ordering. All totals are safe integers. 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
[
[
{
"timestamp": 0,
"service": "api",
"durationMs": 10,
"status": 200
},
{
"timestamp": 9,
"service": "api",
"durationMs": 20,
"status": 500
},
{
"timestamp": 10,
"service": "api",
"durationMs": 10,
"status": 200
}
],
10
]Return value
[
{
"start": 0,
"service": "api",
"count": 2,
"errors": 1,
"totalMs": 30
},
{
"start": 10,
"service": "api",
"count": 1,
"errors": 0,
"totalMs": 10
}
]Example 2
Function arguments
[
[
{
"timestamp": 3,
"service": "z",
"durationMs": 10,
"status": 200
},
{
"timestamp": 1,
"service": "a",
"durationMs": 10,
"status": 200
}
],
5
]Return value
[
{
"start": 0,
"service": "a",
"count": 1,
"errors": 0,
"totalMs": 10
},
{
"start": 0,
"service": "z",
"count": 1,
"errors": 0,
"totalMs": 10
}
]Example 3
Function arguments
[
[
{
"timestamp": 0,
"service": "api",
"durationMs": 1,
"status": 503
}
],
100
]Return value
[
{
"start": 0,
"service": "api",
"count": 1,
"errors": 1,
"totalMs": 1
}
]