Looking for good programming challenges?

Use the search below to find our solutions for selected questions!

Delta encoding challenge

Sharing is caring!

Problem statement
Given a list of numbers, e.g.:

Output a delta encoding for the sequence. In a delta encoding, the first element is reproduced as is. Each subsequent element is represented as the numeric difference from the element before it. E.g. for the sequence above, the delta encoding would be:

However, if a difference value does not fit in a single signed byte, i.e. -127 \le x \le 127, then, instead of the difference, we would like to use an escape token, printing it.

This will denote that the value following the escape token is a full four-byte difference value, rather than a one-byte different value.

For this exercise, we’ll declare -128 as the escape token.

Following the same example above, the final result would be: