Skip to content

46. Rebuild a Table from Its Transaction Log

Difficulty: Medium · Topics: Joins, Aggregations, Filtering & Selection

A Delta table's log records every commit as actions: version, action ('add' or 'remove') and the data file path. The table at a version is the set of files whose latest action at or before that version is an add.

as_of holds a single row with the version to read (time travel). Return the path of every live file at that version.

Row order does not matter; column names must match. Your code is graded on 4 test cases, including hidden edge cases.

Sample data

log

versionactionpath
0addpart-000.parquet
0addpart-001.parquet
1addpart-002.parquet
2removepart-001.parquet
2addpart-003.parquet
3removepart-000.parquet
3removepart-002.parquet
3removepart-003.parquet
3addpart-004.parquet

as_of

version
2

Expected output

path
part-000.parquet
part-002.parquet
part-003.parquet

Hints

Hint 1Keep only log rows with version <= the requested version: cross join the one-row as_of table, or join on a condition.
Hint 2For each path you need the action of its latest commit. F.max_by("action", "version") does that in one aggregate.
Hint 3A file can be removed and later added back; only the latest action counts.

Learn the concepts

PySpark functions you'll practise

Related problems

Browse

Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings

Difficulty: Easy · Medium · Hard · PySpark interview roadmap · Learn · All problems