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

api_calls: 10 of 670 rows
call_idendpointmethodstatuslatencyuser_idcall_timeerr_msg
8079/api/v1/ordersPOST2004.21002026-02-02 01:07:00NULL
8158/api/v2/productsPUT2007.91972026-03-03 02:14:00NULL
8237/api/v1/searchDELETE20111.62942026-04-04 03:21:00NULL
8316/api/v1/auth/loginPATCH20415.33912026-05-05 04:28:00NULL
8395/api/v1/auth/logoutget30119NULL2026-06-06 05:35:00NULL
8474/api/v2/analyticspost40022.75852026-07-07 06:42:00Not found
8553/api/v1/paymentsGET40126.46822026-08-08 07:49:00Unauthorized
8632/api/v1/users/POST40330.17792026-09-09 08:56:00Rate limit exceeded
8711/api/v2/products/PUT40433.88762026-01-10 09:03:00Internal server error
8790/api/v1/usersDELETE50037.5NULL2026-02-11 10:10:00Bad gateway

Expected output

First 5 of 61 rows
call_idendpoints_position
8079/api/v1/orders14
8474/api/v2/analytics17
8632/api/v1/users/10
9027/api/v1/search9
9185/api/v1/auth/logout0

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

Case-folded POST filter with a 1-based string search
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