Some sensor readings have a NULL temp. Forward-fill them: temp_filled is the temp of the same row if present, otherwise the most recent earlier non-NULL temp of the same sensor (ordered by reading_ts; timestamps are unique per sensor). Leading NULLs (before the sensor's first real reading) stay NULL.
| sensor_id | reading_ts | temp |
|---|---|---|
| 1 | 2024-01-01 00:00:00 | 21.5 |
| 1 | 2024-01-01 01:00:00 | NULL |
| 1 | 2024-01-01 02:00:00 | NULL |
| 1 | 2024-01-01 03:00:00 | 23.0 |
| 2 | 2024-01-01 00:00:00 | NULL |
| 2 | 2024-01-01 01:00:00 | 19.0 |
| sensor_id | reading_ts | temp_filled |
|---|---|---|
| 1 | 2024-01-01 00:00:00 | 21.5 |
| 1 | 2024-01-01 01:00:00 | 21.5 |
| 1 | 2024-01-01 02:00:00 | 21.5 |
| 1 | 2024-01-01 03:00:00 | 23 |
| 2 | 2024-01-01 00:00:00 | NULL |
| 2 | 2024-01-01 01:00:00 | 19 |
| sensor_id | reading_ts | temp |
|---|---|---|
| 3 | 2024-02-01 10:00:00 | 30.0 |
| 3 | 2024-02-01 10:05:00 | NULL |
| sensor_id | reading_ts | temp_filled |
|---|---|---|
| 3 | 2024-02-01 10:00:00 | 30 |
| 3 | 2024-02-01 10:05:00 | 30 |
Return columns sensor_id, reading_ts, temp_filled. Row order does not matter.
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
‹ Impute Missing Prices with Category Average · Split and Explode Comma-Separated Tags ›
{{ totalCount }} questions · {{ shHardPct }}% Hard. Real PySpark DataFrame and Spark SQL interview questions. Write PySpark in your browser on TechTown’s Spark engine, run sample tests, then submit against hidden tests.
Guests can solve {{ freeLimit }} problems for free. Log in or sign up (free) to unlock everything, keep your streak and appear on the leaderboard. New to a topic? Learn it first in the Spark course.
{{ p.statement }}
{{ p.constraints }}
No submissions yet. Press Submit to judge your code against all tests.
The model solution and step-by-step explanation unlock when you solve this problem or after 3 unsuccessful submissions ({{ shFailsLabel }}). Struggling? Try the hints on the Question tab first.
{{ p.solution }}
Write real PySpark: from pyspark.sql import functions as F, Window, spark.sql(...). It runs in your browser on a lightweight Spark engine, and the same code runs unchanged on Databricks or any Spark cluster. Use df.show() or print() to debug.
Create your free TechTown account to unlock every problem, track your progress, bookmark questions and keep preparing for interviews.