Lag window function
Implement the LAG window function to compute per-row deltas: value - LAG(value) OVER (PARTITION BY sensor ORDER BY ts). Given rows [sensor, ts, value] (value int, may arrive unsorted), for each sensor order by ts ascending and compute the difference between consecutive readings. The first reading of each sensor has no predecessor, so its delta is 0. Assume (sensor, ts) is unique. Return [sensor, ts, delta] rows sorted by sensor ascending then ts ascending. Up to 5000 rows.
Implement
lag_delta(rows: list[list]) → list[list]Examples
in
[[["s",1,10],["s",2,15],["s",3,12]]]out[["s",1,0],["s",2,5],["s",3,-3]]What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 24 min
solution.py
InputExpectedGot
[[["s",1,10],["s",2,15],["s",3,12]]][["s",1,0],["s",2,5],["s",3,-3]]not run yetsample