AlgoMaster Logo
AlgoMasterEncode Time-Series Timestampsmedium

Encode Time-Series Timestamps

medium

Time-series data often arrives at nearly regular intervals. Delta-of-delta encoding stores how those intervals change, turning a steady stream of timestamps into a run of zeros that compresses efficiently.

Design a DeltaOfDeltaEncoder class:

  • DeltaOfDeltaEncoder() creates a stateless encoder.
  • int[] encode(int[] timestamps) returns the delta-of-delta representation.

The output follows these rules:

  1. Keep the first timestamp unchanged.
  2. Store the first gap, timestamps[1] - timestamps[0], as the second value when it exists.
  3. For each later timestamp, store currentGap - previousGap.
Example 1:

Input:

Output:

Explanation: Every gap is 60, so every change after the first gap is 0.

Example 2:

Input:

Output:

Explanation: The gaps are 60 and 70, so the final encoded value is 70 - 60 = 10.

Constraints

  • 1 <= timestamps.length <= 10^5
  • -10^9 <= timestamps[i] <= 10^9
  • timestamps is non-decreasing.
  • Every computed difference fits in a signed 32-bit integer.
  • At most 100 calls are made to encode.
Hints

Loading...
CallReturns
new DeltaOfDeltaEncoder()null
encode([100,160,220,280])[100,60,0,0]

The first gap is 60 and every later gap is also 60, so both changes in the gap are zero.

Run checks these cases. Submit also runs a larger hidden set.