Competing Standards

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

Domain
SQL
Difficulty
hard
Seniority
L5
Asked in
Google

Problem

For each model (mdl_name), find the framework used across the greatest number of its versions, breaking a tie in that count by the higher average accuracy. Sort the resulting model-framework pairs by average accuracy, highest first, and keep every pair whose accuracy places it among the three highest values, including any that share a position.

Every framework has a star model.

Tables

ml_models

Columns: model_id INTEGER, mdl_name TEXT, version TEXT, accuracy REAL, status TEXT, train_at TEXT, framework TEXT

ml_models: 10 of 200 rows
model_idmdl_nameversionaccuracystatustrain_atframework
123rec_enginev1.10.72training2026-02-02 01:00:00TensorFlow
146churn_predv2.00.89archived2026-03-03 02:00:00XGBoost
169spam_filter3.00.61failed2026-04-04 03:00:00sklearn
192img_classifyv2.1-beta0.78validating2026-05-05 04:00:00pytorch
215sentiment1.0.00.95Deployed2026-06-06 05:00:00tensorflow
238demand_fcstv1.00.67deployedNULLPyTorch
261price_optv1.10.84Training2026-08-08 07:00:00TensorFlow
284fraud_detectv2.0NULLarchived2026-09-09 08:00:00XGBoost
307rec_engine3.00.73failed2026-01-10 09:00:00sklearn
330churn_predv2.1-beta0.9validating2026-02-11 10:00:00pytorch

Expected output

First 3 of 3 rows
mdl_nameframeworkavg_acc
churn_predpytorch0.9450000000000001
price_opttensorflow0.905
img_classifyXGBoost0.845

Worked solution and explanation

Why this problem exists in real interviews

Two ranks live inside one prompt and they answer different questions. The per-model choice of framework is a vote over how many of a model's versions use each framework (with accuracy as the tiebreaker), while the cross-model leaderboard is a quality ranking. Conflating them into one window is how this gets botched.


Break down the requirements

Step 1: Per-(model, framework) rollup

Group ml_models by mdl_name and framework. Carry both COUNT(DISTINCT version) as the version count and AVG(accuracy) because the next step needs both. Counting distinct versions, not raw rows, is what the phrase "across the most of its versions" means.

Step 2: Pick the primary framework per model

Inside each mdl_name, order by ver_count DESC, avg_acc DESC and keep ROW_NUMBER() = 1, adding framework as a final tiebreak so the ordering is a total order and the pick is deterministic. ROW_NUMBER, not RANK, because each model returns exactly one framework.

Step 3: Cross-model leaderboard with ties

Apply DENSE_RANK() OVER (ORDER BY avg_acc DESC) to the survivors. DENSE_RANK keeps ranks 1, 2, 3 contiguous when ties widen the result set.

Step 4: Top 3 ranks, not top 3 rows

Filter rnk <= 3. The phrase "include any that share a position" on ties is why the filter is on the rank value, not a LIMIT 3.


The solution

Competing Standards
WITH fw_counts AS (
  SELECT mdl_name, framework,
         COUNT(DISTINCT version) AS ver_count,
         AVG(accuracy) AS avg_acc
  FROM ml_models
  GROUP BY mdl_name, framework
),
primary_fw AS (
  SELECT mdl_name, framework, avg_acc,
         ROW_NUMBER() OVER (
           PARTITION BY mdl_name
           ORDER BY ver_count DESC, avg_acc DESC, framework
         ) AS rn
  FROM fw_counts
),
best AS (
  SELECT mdl_name, framework, avg_acc
  FROM primary_fw
  WHERE rn = 1
),
ranked AS (
  SELECT mdl_name, framework, avg_acc,
         DENSE_RANK() OVER (ORDER BY avg_acc DESC) AS rnk
  FROM best
)
SELECT mdl_name, framework, avg_acc
FROM ranked
WHERE rnk <= 3
ORDER BY avg_acc DESC

COMMON FOLLOW-UP QUESTIONS

Common follow-up questions