Largest A/B Test by Participants

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
L3
Asked in
ESPN

Problem

The data science team is identifying which A/B test drew the widest audience. Return the test with the most unique participants, along with that participant count; if several tests drew the same audience, take the one whose name comes first alphabetically.

The biggest experiment ever run.

Tables

ab_results

Columns: result_id INTEGER, test_name TEXT, variant TEXT, user_id INTEGER, metric TEXT, value REAL, created TEXT

ab_results: 10 of 200 rows
result_idtest_namevariantuser_idmetricvaluecreated
237new_homepagetreatment_a100click_rate13.72026-02-02
274pricing_pagetreatment_b197revenue27.42026-03-03
311onboarding_v3Control294bounce41.12026-04-04
348search_algo_bCONTROL391time_on_page54.82026-05-05
385rec_engine_v2control488signup68.52026-06-06
422checkout_flow_v2treatment_a585conv_rate82.22026-07-07
459new_homepagetreatment_b682click_rate95.92026-08-08
496pricing_pageControl779revenue9.62026-09-09
533onboarding_v3CONTROL876bounce23.32026-01-10
570search_algo_bcontrol973time_on_pageNULL2026-02-11

Expected output

First 1 of 1 rows
test_nameunique_participants
new_homepage17

Worked solution and explanation

Why this problem exists in real interviews

Eight million rows in ab_results and the grain is per-event, not per-person. A power tester who triggered the conversion metric forty times shows up as forty rows. Anyone who writes COUNT(*) is counting telemetry pings and calling it audience reach.


Break down the requirements

Step 1: Aggregate to the human

Group by test_name and reduce per-event rows with COUNT(DISTINCT user_id). A given user_id may appear across variant values or many metric rows in the same test.

Step 2: Rank and slice

Sort descending on the distinct count and take the top entry with LIMIT 1. Several tests can tie on participant count, so add test_name ASC as a tiebreaker to make the winner deterministic: on a tie, the alphabetically earliest name wins.


The solution

TOP TEST BY UNIQUE PARTICIPANTS
SELECT test_name,
       COUNT(DISTINCT user_id) AS unique_participants
FROM ab_results
GROUP BY test_name
ORDER BY unique_participants DESC, test_name LIMIT 1

COMMON FOLLOW-UP QUESTIONS

Common follow-up questions