Poles Apart

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
City and County of San Francisco

Problem

Never-used API tokens all sit at zero requests, so in the security team's review the quiet end is measured only over tokens that have actually been used. From our API tokens, surface the busiest and the quietest tokens along with every admin-scoped token that has never been used at all, each with its request count, busiest first.

Two extremes, one result: the busiest tokens and the quietest.

Tables

api_tokens

Columns: token_id INTEGER, owner_id INTEGER, scope TEXT, status TEXT, issued TEXT, expires TEXT, last_used TEXT, requests INTEGER

api_tokens: 10 of 200 rows
token_idowner_idscopestatusissuedexpireslast_usedrequests
1043197writerevoked2025-02-022027-02-022026-02-0247
1086294adminexpired2025-03-032027-03-032026-03-0394
1129391read:usersActive2025-04-042027-04-042026-04-04141
1172488write:ordersREVOKED2025-05-052027-05-052026-05-05188
1215585read:analyticsactive2025-06-062027-06-062026-06-06235
1258682full_accessrevoked2025-07-07NULL2026-07-07282
1301779readexpired2025-08-082027-08-082026-08-08329
1344876writeActive2025-09-092027-09-09NULL0
1387973adminREVOKED2025-10-102027-10-102026-01-10423
14301070read:usersactive2025-11-112027-11-112026-02-11470

Expected output

First 5 of 8 rows
token_idrequests
53004700
100054004700
104347
1000530147
16880

Worked solution and explanation

What this problem really is

Beneath the security-audit framing, this is three conditions folded into one WHERE, each scoped differently: the global MAX(requests), the MIN(requests) taken only over tokens that have been used, and a dormant-privilege flag. Scoping each one correctly is the whole difficulty. Every never-used token carries requests = 0, so a naive MIN(requests) over the full table comes back 0, and your 'quietest' list becomes tokens that never served a call. The busy side has no such trap: an unused token can never be the busiest, so guarding it does nothing. The dormant branch, last_used IS NULL AND scope = 'admin', only means what you want because AND binds tighter than the surrounding OR. Drop either predicate and the flag pulls in every admin token or every dormant one.


Working it out

Step 1: Take the busy end straight

The busiest extreme is a plain MAX(requests) scalar subquery over the whole table. A never-used token sits at 0, so it can never be the maximum. Adding a used-only filter here removes nothing and hides the point.

Step 2: Scope the quiet end to used tokens

The quiet extreme is MIN(requests) taken over last_used IS NOT NULL. Skip that guard and the minimum is 0, contributed by tokens that never ran, and they swamp the answer. This is the one place the null check earns its keep.

Step 3: Flag the dormant admin keys

A token that has never been used but still carries an admin scope is a risk of its own. Express it as last_used IS NULL AND scope = 'admin'. Both predicates pull their weight: without the null check you flag every admin token, and without the scope check you flag every dormant token.

Step 4: Combine and order

Join the three conditions with OR so every token at an extreme or flagged as dormant admin comes back, and wrap the AND branch in parentheses so the grouping is explicit. Then ORDER BY requests DESC so the busiest lead. Tokens tied on a count can come back in any order.


The solution

Three scopes, one WHERE
SELECT token_id, requests
FROM api_tokens
WHERE requests = (SELECT MAX(requests) FROM api_tokens)
   OR requests = (SELECT MIN(requests) FROM api_tokens WHERE last_used IS NOT NULL)
   OR (last_used IS NULL AND scope = 'admin')
ORDER BY requests DESC

Common follow-up questions