Parallel Traces

A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
medium
Seniority
L4
Asked in
Airbnb

Problem

The experimentation team is looking for cross-variant contamination. Pair users who participated in the same experiment under different variants but on the same platform. Show both user IDs for each qualifying pair, running the list up from the lowest-numbered user, and within each user up from their lowest-numbered partner.

Same experiment. Different variants. Who overlaps?

Tables

experiments

Columns: exp_id INTEGER, exp_name TEXT, variant TEXT, user_id INTEGER, outcome REAL, created TEXT, platform TEXT

experiments: 10 of 200 rows
exp_idexp_namevariantuser_idoutcomecreatedplatform
1053checkout_v3variant_a10011.32026-02-02android
1106onboard_flowvariant_b19722.62026-03-03web
1159pricing_tiervariant_c29433.92026-04-04iOS
1212search_rankholdout39145.22026-05-05Android
1265rec_model_bcontrol48856.52026-06-06NULL
1318notif_freqvariant_a585NULL2026-07-07WEB
1371home_layoutvariant_b68279.12026-08-08NULL
1424new_feed_algovariant_c77990.42026-09-09ios
1477checkout_v3holdout8761.72026-01-10android
1530onboard_flowcontrol973132026-02-11web

Expected output

First 5 of 382 rows
user_id1user_id2
100876
1001652
1002428
1003204
1004756

Worked solution and explanation

What this problem really is

This is a self-join with an inequality dressed up as experiment-contamination detection. The skill being probed: can you join a table to itself so each qualifying pair of users surfaces exactly once? Anyone can match rows on the same exp_name and platform with differing variants. The trap is the pair math: without e1.user_id < e2.user_id, every pair comes back twice (once in each order) and every row also matches itself. Get that wrong and your contamination report double-counts every collision and flags users as colliding with themselves.


Break down the requirements

Step 1: Line up the same experiment on the same platform

Join experiments to a second copy of itself, keeping only rows that share the same exp_name and the same platform. This is the surface where two participations could collide.

Step 2: Force the variants to differ

Add variant != variant so the two sides are genuinely different treatments. Same variant is not contamination, it is just the same arm of the test.

Step 3: Emit each pair once

Add user_id < user_id. This is the quiet workhorse: it drops the self-match (a row against itself) and collapses the two mirror-image orderings of a pair down to one. DISTINCT then absorbs any repeat participation by the same users.


The solution

Self-join with an inequality for unique pairs
SELECT DISTINCT e1.user_id AS user_id1, e2.user_id AS user_id2
FROM experiments e1
JOIN experiments e2
  ON e1.exp_name = e2.exp_name
  AND e1.platform = e2.platform
  AND e1.variant != e2.variant
  AND e1.user_id < e2.user_id
ORDER BY user_id1, user_id2

Common follow-up questions