Skip to content

68. What Changed Between Two Versions

Difficulty: Hard · Topics: Joins, Strings, Null Handling, Conditional Logic, Filtering & Selection

v1 and v2 are the same customers table read at two versions (time travel), each with id, email, city and tier. Any of the three attributes can be null.

Return one row per id that differs: id, change ('added', 'removed' or 'changed') and changed_cols, the names of the attributes whose values differ, comma-separated in the order email,city,tier (null for added and removed rows). A change from null to a value, or back, counts as a change. Unchanged ids are left out.

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

Sample data

v1

idemailcitytier
1a@x.comPunefree
2b@x.comDelhipro
3nullAgrafree
4d@x.comKochipro

v2

idemailcitytier
1a@x.comPunefree
2b@new.comDelhienterprise
3c@x.comAgrafree
5e@x.comGoafree

Expected output

idchangechanged_cols
2changedemail,tier
3changedemail
4removednull
5addednull

Hints

Hint 1Rename the attributes of each side (for example email_1, email_2) and full-outer-join on id.
Hint 2a != b is null when either side is null, so it misses null changes. Use ~a.eqNullSafe(b).
Hint 3F.concat_ws(",", ...) skips null arguments, so F.when(changed, F.lit("email")) without otherwise builds the list. An empty list means unchanged.

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