Character Position in Endpoint
An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- easy
- Seniority
- L3
- Asked in
- Square
Problem
The HTTP method in api_calls was logged inconsistently, sometimes uppercase and sometimes lowercase, and the API team's audit of POST traffic needs every one of those requests whichever way it was spelled. For each, return its call ID and endpoint alongside the position where the letter 's' first appears in the endpoint, in call ID order, lowest first.
URL patterns, character by character.
Tables
api_calls
Columns: call_id INTEGER, endpoint TEXT, method TEXT, status INTEGER, latency REAL, user_id INTEGER, call_time TEXT, err_msg TEXT
| call_id | endpoint | method | status | latency | user_id | call_time | err_msg |
|---|---|---|---|---|---|---|---|
| 8079 | /api/v1/orders | POST | 200 | 4.2 | 100 | 2026-02-02 01:07:00 | NULL |
| 8158 | /api/v2/products | PUT | 200 | 7.9 | 197 | 2026-03-03 02:14:00 | NULL |
| 8237 | /api/v1/search | DELETE | 201 | 11.6 | 294 | 2026-04-04 03:21:00 | NULL |
| 8316 | /api/v1/auth/login | PATCH | 204 | 15.3 | 391 | 2026-05-05 04:28:00 | NULL |
| 8395 | /api/v1/auth/logout | get | 301 | 19 | NULL | 2026-06-06 05:35:00 | NULL |
| 8474 | /api/v2/analytics | post | 400 | 22.7 | 585 | 2026-07-07 06:42:00 | Not found |
| 8553 | /api/v1/payments | GET | 401 | 26.4 | 682 | 2026-08-08 07:49:00 | Unauthorized |
| 8632 | /api/v1/users/ | POST | 403 | 30.1 | 779 | 2026-09-09 08:56:00 | Rate limit exceeded |
| 8711 | /api/v2/products/ | PUT | 404 | 33.8 | 876 | 2026-01-10 09:03:00 | Internal server error |
| 8790 | /api/v1/users | DELETE | 500 | 37.5 | NULL | 2026-02-11 10:10:00 | Bad gateway |
Expected output
| call_id | endpoint | s_position |
|---|---|---|
| 8079 | /api/v1/orders | 14 |
| 8474 | /api/v2/analytics | 17 |
| 8632 | /api/v1/users/ | 10 |
| 9027 | /api/v1/search | 9 |
| 9185 | /api/v1/auth/logout | 0 |
Worked solution and explanation
What this is really testing
Beneath the audit story, this is a case-normalized filter feeding a 1-based string search. Anyone can call a position function. What separates people is the method column: it was logged inconsistently, so 'POST' and 'post' both live in the data. Match the value as stored and you silently keep only one spelling, reporting on barely half the POST requests, with every position computed for the wrong slice.
Working through it
Step 1: Fold the method's case
Nothing normalized the method on write, so the same verb shows up as 'POST' and 'post'. LOWER(method) = 'post' collapses both onto one value, so every POST request qualifies instead of just one casing.
Step 2: Locate the first 's'
INSTR(endpoint, 's') returns the 1-based position of the first 's'. The first character is position 1, not 0, and an endpoint with no 's' at all comes back as 0 rather than disappearing from the result.
Step 3: Report one row per request
Keep call_id in the SELECT so every request is its own row. Many POST calls hit the same endpoint, so the output is per request, not one row per path.
Step 4: Sort by call_id
call_id is unique, so ORDER BY call_id gives a total order: no two rows can trade places, and the grader has exactly one correct sequence to check.
The solution
SELECT call_id, endpoint, INSTR(endpoint, 's') AS s_position
FROM api_calls
WHERE LOWER(method) = 'post'
ORDER BY call_id- Matches the raw value
WHERE method = 'post'matches only the lowercase rows and misses every 'POST', so the audit silently runs on a fraction of the traffic.- Folds the case first
WHERE LOWER(method) = 'post'collapses both spellings onto one value, so every POST request appears exactly once.
Common follow-up questions
- How would you find the position of the second 's', not just the first? (Tests `SUBSTR` and `INSTR` composition for repeated matches.)
- How would you collapse this to one row per `endpoint` with a count of POST calls? (Tests grouping and aggregation over the row-level result.)
- What changes if some endpoints were also logged with uppercase letters? (Tests whether the candidate also normalizes the searched column, not just the filtered one.)