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 DATE

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

This looks like a leaderboard, but it is really a distinct count over a table whose rows are events. Every row in ab_results is one logged metric result for one user, and here each participant shows up on 2 rows of their test. Anyone who writes COUNT(*) reports 34 for the winner instead of 17, counting telemetry and calling it audience reach. On a production table where some tests log far more metrics per user than others, the same shortcut can also crown the wrong test.


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, and it should count once.

Step 2: Rank and slice

Sort descending on the distinct count and take the top entry with LIMIT 1. Here 4 tests tie at 17 participants, so the alphabetical rule decides the winner: add test_name ASC as the second key and 'new_homepage' comes out on top every time.


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