KatInterview

Repair a Sensor Log: Merge, Sort, and Interpolate Gaps

Arrays & HashingAuto-graded · 22 test cases
EasyAuto-gradedArrays & Hashinghash-mapsortinginterpolation

Last updated

Merge duplicate timestamps by averaging known readings, sort, then fill missing values by linear interpolation inside gaps and nearest-value holding at the ends.

Repair a Sensor Log: Merge, Sort, and Interpolate Gaps

A sensor produces a log of readings. Each entry is a pair [timestamp, value], where timestamp is an integer and value is either a floating-point number or null, which means the reading was lost.

The log may be in any order, and a timestamp can show up in several entries. Produce a repaired log by following these rules in order.

  1. Combine entries with the same timestamp.

    • If at least one entry for that timestamp has a non-null value, the combined value is the arithmetic mean of the non-null values only. Entries with null are ignored in the average.
    • If every entry for that timestamp is null, the timestamp becomes one missing point.
  2. Sort the combined points by timestamp, ascending.

  3. Interpolate inside gaps. Take a missing point at time t that has a known point somewhere before it and a known point somewhere after it. Let (t1, v1) be the closest known point with t1 < t, and (t2, v2) the closest known point with t2 > t. Its value is

    v = v1 + (v2 - v1) * (t - t1) / (t2 - t1)

    Only original known values (after step 1) are used as v1 and v2, never values that were filled in.

  4. Hold values at the ends. A missing point earlier than every known point takes the value of the earliest known point. A missing point later than every known point takes the value of the latest known point.

Return the repaired log as a list of [timestamp, value] pairs, one per distinct timestamp, sorted by timestamp ascending, with every value a float.

Input

  • records: a list of n pairs [timestamp, value], where value is a float or null (None in Python).

Output

  • A list of [timestamp, value] pairs, one per distinct timestamp, in ascending timestamp order, with all values filled in as floats.

Constraints

  • 1 <= n <= 10^5
  • -10^9 <= timestamp <= 10^9
  • Known values satisfy -10^6 <= value <= 10^6
  • At least one record has a known (non-null) value.
  • Answers within an absolute error of 10^-6 are accepted.

Examples

Example 1

Input: repair_sensor_log([[3, None], [1, 10], [5, 20], [3, None]])

Output: [[1, 10], [3, 15], [5, 20]]

Example 2

Input: repair_sensor_log([[2, 4], [2, 6], [4, None], [6, 8], [0, None]])

Output: [[0, 5], [2, 5], [4, 6.5], [6, 8]]

Hints

Hint 1

Group the records by timestamp, keeping a running sum and a count of only the non-null values. A count of zero means the timestamp is a missing point.

Hint 2

After sorting the distinct timestamps, collect the indices of the known points. Every missing point lies either before the first known index, after the last, or between two consecutive known indices.

Hint 3

For each pair of consecutive known indices, the left and right ones are exactly the nearest known neighbours of every missing point between them, so you can interpolate that whole stretch in one pass without searching.