How do you carry the last value forward (LOCF) to fill gaps in a time series with SQL window functions?

Quick Overview

Asks how to fill NULL gaps in a time series by carrying the last observed value forward using SQL window functions. Tests window function frames, partitioning by series, handling of ties and leading NULLs, and awareness of engine-specific options such as IGNORE NULLS.

How do you carry the last value forward (LOCF) to fill gaps in a time series with SQL window functions?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Explain how to carry a value forward to fill gaps (last observation carried forward, or LOCF) in a time series using SQL window functions. Assume a table of readings where some rows have a NULL value, and each NULL should take the most recent non-NULL value that came before it in the same series. ```hint Group the gap with its source A run of NULL rows belongs with the last real value before it. Look for a running quantity that stays constant across that run and changes only when a real value appears. ``` ```hint Pitfalls Check what your query does when the table holds several series, when a series starts with NULLs, and when two rows share a timestamp. ``` ### What a Strong Answer Covers - Why `LAG()` or a running `MAX()` alone does not produce LOCF - A portable query that works without engine-specific extensions, and how it assigns each NULL to the right value - Partitioning by series and choosing an ordering and frame that give a deterministic result - Behavior for leading NULLs, and the `IGNORE NULLS` shortcut where the engine supports it ### Follow-up Questions - How would you carry a value forward only for a limited time, for example at most 2 hours after the reading? - The table has no row at all for some timestamps. How do you build a regular hourly series per sensor and then fill it? - How would you fill backward instead (next observation carried backward)? - When is LOCF a poor imputation choice for analysis?

Overview: Asks how to fill NULL gaps in a time series by carrying the last observed value forward using SQL window functions. Tests window function frames, partitioning by series, handling of ties and leading NULLs, and awareness of engine-specific options such as IGNORE NULLS.

Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Explain how to carry a value forward to fill gaps (last observation carried forward, or LOCF) in a time series using SQL window functions.

Assume a table of readings where some rows have a NULL value, and each NULL should take the most recent non-NULL value that came before it in the same series.

What a Strong Answer Covers Guidance

  • Why LAG() or a running MAX() alone does not produce LOCF
  • A portable query that works without engine-specific extensions, and how it assigns each NULL to the right value
  • Partitioning by series and choosing an ordering and frame that give a deterministic result
  • Behavior for leading NULLs, and the IGNORE NULLS shortcut where the engine supports it

Follow-up Questions Guidance

  • How would you carry a value forward only for a limited time, for example at most 2 hours after the reading?
  • The table has no row at all for some timestamps. How do you build a regular hourly series per sensor and then fill it?
  • How would you fill backward instead (next observation carried backward)?
  • When is LOCF a poor imputation choice for analysis?