# Poles Apart

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

Canonical URL: <https://datadriven.io/problems/poles_apart>

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.

## Tables

### api_tokens

- token_id INTEGER
- owner_id INTEGER
- scope TEXT
- status TEXT
- issued TEXT
- expires TEXT
- last_used TEXT
- requests INTEGER

Sample rows (10 of 200):

| token_id | owner_id | scope | status | issued | expires | last_used | requests |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 1043 | 197 | write | revoked | 2025-02-02 | 2027-02-02 | 2026-02-02 | 47 |
| 1086 | 294 | admin | expired | 2025-03-03 | 2027-03-03 | 2026-03-03 | 94 |
| 1129 | 391 | read:users | Active | 2025-04-04 | 2027-04-04 | 2026-04-04 | 141 |
| 1172 | 488 | write:orders | REVOKED | 2025-05-05 | 2027-05-05 | 2026-05-05 | 188 |
| 1215 | 585 | read:analytics | active | 2025-06-06 | 2027-06-06 | 2026-06-06 | 235 |
| 1258 | 682 | full_access | revoked | 2025-07-07 | NULL | 2026-07-07 | 282 |
| 1301 | 779 | read | expired | 2025-08-08 | 2027-08-08 | 2026-08-08 | 329 |
| 1344 | 876 | write | Active | 2025-09-09 | 2027-09-09 | NULL | 0 |
| 1387 | 973 | admin | REVOKED | 2025-10-10 | 2027-10-10 | 2026-01-10 | 423 |
| 1430 | 1070 | read:users | active | 2025-11-11 | 2027-11-11 | 2026-02-11 | 470 |

## Expected output

First 5 of 8 rows:

| token_id | requests |
| --- | --- |
| 5300 | 4700 |
| 10005400 | 4700 |
| 1043 | 47 |
| 10005301 | 47 |
| 1688 | 0 |

## 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**

```sql
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
```

> **One guard, one place**
>
> Two slips show up. The first is mirror-guarding: adding `last_used IS NOT NULL` to the outer query and to the `MAX` subquery looks careful but changes nothing, because a never-used token is never the busiest. The second is precedence: reading the last branch as `(... OR last_used IS NULL) AND scope = 'admin'`. `AND` binds tighter than `OR`, so the dormant flag is `(last_used IS NULL AND scope = 'admin')`; the parentheses just make that grouping visible.

> **They watch for the scoping asymmetry**
>
> The tell is whether you noticed that each condition lives at a different scope: the null guard belongs on the `MIN`, not the `MAX`, and the dormant flag needs both of its predicates. Strong candidates also mention that the two extremes could come from a single scan with `MIN(requests) OVER ()` and `MAX(requests) OVER ()`.

> **Cost stays flat**
>
> In production, `api_tokens` is roughly 250,000 rows. The `MAX` and `MIN` scalar subqueries are one scan each, and both collapse to an index seek if `requests` is indexed; the outer predicate is a third cheap scan. At this size the plan cost is negligible, and getting the scoping right matters far more.

---

## Common follow-up questions

- How would you return both extremes from a single scan instead of two subqueries? _(Tests fluency with `MIN(requests) OVER ()` and `MAX(requests) OVER ()`; the null guard then moves into a filtered window or a pre-filtered CTE.)_
- What changes if the dormant-privilege rule covered `admin` or `full_access` scopes? _(A small change to an `IN` list; the candidate should confirm that the AND/OR precedence and the OR semantics still hold.)_
- What if the spec became the top five and bottom five used tokens instead of the single extremes? _(Forces `DENSE_RANK` or ranked CTEs unioned together to keep ties at each boundary.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/poles_apart)
- [SQL Interview Questions](https://datadriven.io/sql-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). DataDriven is the data engineering interview community. Live code execution in SQL, Python, and Spark sandboxes. Every feature is open to every member.