Problem · Array

Chunked Byte Stream Checksums

Learn this problem
EasyUpstart logoUpstartFULLTIMEOA

Problem statement

An integer array stream encodes consecutive chunks. Each chunk starts with one header byte k, followed by exactly k data bytes.

For every chunk, compute the unsigned byte-sum checksum:

checksum = (sum of the chunk's data bytes) modulo 256

The header byte is not part of the checksum. Return the checksums in chunk order. A zero-length chunk has checksum 0.

Function

computeChecksums(stream: int[]) → int[]

Examples

Example 1

stream = [3,10,20,30,2,255,2]return = [60,1]

The first chunk sums to 60. The second sums to 257, whose remainder modulo 256 is 1.

Example 2

stream = [0,1,7,4,100,100,100,100]return = [0,7,144]

The chunks contain zero, one, and four data bytes. The last checksum is 400 modulo 256 = 144.

Constraints

  • 1 <= stream.length <= 10^5.
  • Every value is an integer from 0 through 255.
  • The stream is valid: each header is followed by exactly its declared number of data bytes, and the final chunk ends at the end of the array.

More Upstart problems

drafts saved locally
public int[] computeChecksums(int[] stream) {
    // Write your code here.
}
stream[3,10,20,30,2,255,2]
expected[60,1]
checking account