Mô tả bài toán
Viết function solution(events, width). Đầu vào và đầu ra là giá trị JSON. Không sửa mảng hoặc đối tượng đầu vào. Không hỗ trợ thư viện ngoài, truy cập mạng hoặc hàm bất đồng bộ. 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. Giới hạn đầu vào: tối đa 50.000 phần tử mỗi mảng và 1.000 ký tự mỗi chuỗi. Số phải hữu hạn; số nguyên và tổng số nguyên nằm trong phạm vi số nguyên an toàn của JavaScript. Các kiểu và quy tắc hợp lệ khác theo mô tả ở trên.
Ví dụ đầu vào và đầu ra
Ví dụ 1
Đối số của hàm
[
[
{
"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
]Giá trị trả về
[
{
"start": 0,
"service": "api",
"count": 2,
"errors": 1,
"totalMs": 30
},
{
"start": 10,
"service": "api",
"count": 1,
"errors": 0,
"totalMs": 10
}
]Ví dụ 2
Đối số của hàm
[
[
{
"timestamp": 3,
"service": "z",
"durationMs": 10,
"status": 200
},
{
"timestamp": 1,
"service": "a",
"durationMs": 10,
"status": 200
}
],
5
]Giá trị trả về
[
{
"start": 0,
"service": "a",
"count": 1,
"errors": 0,
"totalMs": 10
},
{
"start": 0,
"service": "z",
"count": 1,
"errors": 0,
"totalMs": 10
}
]Ví dụ 3
Đối số của hàm
[
[
{
"timestamp": 0,
"service": "api",
"durationMs": 1,
"status": 503
}
],
100
]Giá trị trả về
[
{
"start": 0,
"service": "api",
"count": 1,
"errors": 1,
"totalMs": 1
}
]