Fix the truncation report for a UDP collector

Review the AI's codeEasy10 min

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_total is the sum.
  • A datagram is truncated when its size is greater than buf_size. A datagram exactly the buffer size fits.
  • lost_bytes is the sum, over truncated datagrams, of size minus buf_size.
  • min_buffer_needed is 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
Inputrecv_report([100, 512, 1472], 512)
Output[1124, 1, 960, 1472]
Example 2
Inputrecv_report([64, 128], 512)
Output[192, 0, 0, 128]
Example 3
Inputrecv_report([1472], 512)
Output[512, 1, 960, 1472]