Code Room
CodingMediumcod-g1169
Subject Database window functionsLevel Mid–Senior~24 minCommon in Databases & SQL interviewsIndustries Software development

Question

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.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.