Bargains and Budget-Busters

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
L4

Problem

FinOps is auditing cloud spend region by region. Two systems record line items: the provider's cost ledger and the team's internal allocations. Treat them as one combined stream of line items. For each region, name the service behind the single biggest line item and the service behind the single smallest. Return one row per region with the region, its biggest spender, and its cheapest line item.

Every region has both. Find them.

Tables

cloud_costs

Columns: cost_id INTEGER, provider TEXT, svc_name TEXT, region TEXT, amount REAL, acct_id TEXT, bill_date TEXT

cloud_costs: 10 of 200 rows
cost_idprovidersvc_nameregionamountacct_idbill_date
543GCPS3us-west-219.33acct-10012026-02-01
586AzureRDSeu-west-137.16acct-10022026-03-01
629awsLambdaap-southeast-154.99acct-10032026-04-01
672gcpBigQueryus-central172.82acct-10042026-05-01
715AWSCloud Runeurope-west190.65acct-10052026-06-01
758GCPBlob Storageus-east-1108.48acct-10062026-07-01
801AzureCosmos DBus-west-2126.31acct-10072026-08-01
844awsCloudFronteu-west-1144.14acct-10082026-09-01
887gcpPub/Subap-southeast-1161.97NULL2026-01-01
930AWSEC2us-central1179.8acct-10102026-02-01

cost_allocs

Columns: alloc_id INTEGER, team_name TEXT, svc_name TEXT, region TEXT, amount REAL, period TEXT, acct_id TEXT, category TEXT

cost_allocs: 10 of 200 rows
alloc_idteam_namesvc_nameregionamountperiodacct_idcategory
129data-engS3us-west-283.212026-02acct-101storage
158ml-teamRDSeu-west-1156.422026-03acct-102network
187frontendLambdaap-south-1229.632026-04acct-103database
216backendBigQueryus-central1-120.52026-05acct-104ml
245devopsCloudFronteurope-west1376.052026-06acct-105other
274securityEKSap-southeast-1449.262026-07acct-106compute
303mobileSQSeu-central-1522.472026-08acct-107storage
332PlatformEC2us-east-1595.682026-09NULLnetwork
361DATA-ENGS3us-west-2668.892026-01acct-109database
390platformRDSeu-west-1742.12026-02acct-110ml

Expected output

First 5 of 8 rows
regionmost_expensivecheapest
ap-south-1RDSLambda
ap-southeast-1CloudFrontCloud Run
eu-central-1EKSSQS
eu-west-1S3CloudFront
europe-west1BigQueryCloud Run

Worked solution and explanation

Why this problem exists in real interviews

Two tables, one combined stream, two extreme picks per group. The interviewer is checking three things: that you UNION ALL the heterogeneous sources rather than treating one as canonical, that you encode the alphabetical tie-break the prompt specifies into the window's ORDER BY rather than letting the engine pick, and that you reach for FIRST_VALUE within a region partition instead of aggregating away the underlying line items.


Break down the requirements

Step 1: Combine the two sources

UNION ALL cloud_costs and cost_allocs, projecting just region, svc_name, amount from each. The two tables record line items from different sources (provider-billed vs. internally allocated), so a row in one is not a duplicate of the other; UNION ALL preserves every line item, while UNION would deduplicate identical (region, svc, amount) tuples and silently lose data.

Step 2: Compute the two extremes per region with FIRST_VALUE

Inside a CTE, attach two window-function columns: FIRST_VALUE(svc_name) OVER (PARTITION BY region ORDER BY amount DESC, svc_name ASC) and FIRST_VALUE(svc_name) OVER (PARTITION BY region ORDER BY amount ASC, svc_name ASC). The secondary svc_name ASC makes the result deterministic when two line items in a region share the extreme amount; without it, FIRST_VALUE returns whichever row the engine sees first. Each row in the CTE now carries its region's most_expensive and cheapest service names.

Step 3: Collapse to one row per region

SELECT DISTINCT region, most_expensive, cheapest collapses the row count to one per region, since the two window-derived columns are constant within a region partition.


The solution

UNION ALL, `FIRST_VALUE` per region, then SELECT DISTINCT
WITH combined AS (
    SELECT region, svc_name, amount FROM cloud_costs
    UNION ALL
    SELECT region, svc_name, amount FROM cost_allocs
),
ranked AS (
    SELECT
        region,
        svc_name,
        amount,
        FIRST_VALUE(svc_name) OVER (PARTITION BY region ORDER BY amount DESC, svc_name ASC) AS most_expensive,
        FIRST_VALUE(svc_name) OVER (PARTITION BY region ORDER BY amount ASC,  svc_name ASC) AS cheapest
    FROM combined
)
SELECT DISTINCT region, most_expensive, cheapest
FROM ranked

Common follow-up questions