customer_updates holds every change of a customer's city (customer_id + updated_at is unique). Turn it into an SCD Type-2 history: valid_from = updated_at, valid_to = the day BEFORE the customer's next update (NULL for the latest row), and is_current = true only for each customer's latest row.
| customer_id | city | updated_at |
|---|---|---|
| 101 | Jaipur | 2023-01-01 |
| 101 | Pune | 2023-06-15 |
| 101 | Delhi | 2024-02-01 |
| 102 | Mumbai | 2023-03-03 |
| customer_id | city | valid_from | valid_to | is_current |
|---|---|---|---|---|
| 101 | Jaipur | 2023-01-01 | 2023-06-14 | False |
| 101 | Pune | 2023-06-15 | 2024-01-31 | False |
| 101 | Delhi | 2024-02-01 | NULL | True |
| 102 | Mumbai | 2023-03-03 | NULL | True |
| customer_id | city | updated_at |
|---|---|---|
| 103 | Kota | 2024-01-01 |
| 103 | Ajmer | 2024-01-02 |
| customer_id | city | valid_from | valid_to | is_current |
|---|---|---|---|---|
| 103 | Kota | 2024-01-01 | 2024-01-01 | False |
| 103 | Ajmer | 2024-01-02 | NULL | True |
Return columns customer_id, city, valid_from, valid_to, is_current. Row order does not matter.
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
‹ Average Order Value by Customer Segment · Year-over-Year Growth by Category ›
{{ 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.