Fix the truncation report for a UDP collector
Your task
Your telemetry collector receives UDP datagrams into a fixed buffer. When a datagram is larger than the buffer, the kernel copies only what fits, drops the rest and reports no error, so large messages arrive "corrupted". You are writing an audit that says how much was lost and how big the buffer should be.
An assistant wrote recv_report(sizes, buf_size). It is in the editor. It has a bug. Find it and fix it.
Input and output:
sizes: the size in bytes of each datagram that arrived, in order. Each is 0 or more.buf_size: the receive buffer size in bytes, 0 or more.- Return four numbers:
[delivered_total, truncated_count, lost_bytes, min_buffer_needed].
Rules the tests check:
- Each datagram delivers its size or
buf_size, whichever is smaller.delivered_totalis the sum. - A datagram is truncated when its size is greater than
buf_size. A datagram exactly the buffer size fits. lost_bytesis the sum, over truncated datagrams, of size minusbuf_size.min_buffer_neededis the size of the largest datagram that arrived, so a buffer that big would have held them all. With no datagrams it is 0.
Examples
recv_report(sizes: list[int], buf_size: int) -> list[int]Example 1
Input
recv_report([100, 512, 1472], 512)Output
[1124, 1, 960, 1472]Example 2
Input
recv_report([64, 128], 512)Output
[192, 0, 0, 128]Example 3
Input
recv_report([1472], 512)Output
[512, 1, 960, 1472]