Gateway Connection Timeouts
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
- TripAdvisor
Problem
During an on-call investigation, find all error tracking entries where the service name contains 'gateway' and the message contains the phrase 'timed out'.
Timeouts at the gateway.
Tables
err_tracks
Columns: err_id INTEGER, err_type TEXT, message TEXT, svc_name TEXT, severity TEXT, count INTEGER, first_at TEXT
| err_id | err_type | message | svc_name | severity | count | first_at |
|---|---|---|---|---|---|---|
| 137 | TypeError | Cannot read property of undefined | payment-api | error | 8 | 2026-02-02 01:11:00 |
| 174 | ConnectionTimeout | Connection timed out after 30s | user-svc | warning | 15 | 2026-03-03 02:22:00 |
| 211 | OutOfMemory | Heap space exhausted | search-api | info | 22 | 2026-04-04 03:33:00 |
| 248 | FileNotFound | File /tmp/data.csv not found | gateway | Fatal | 29 | 2026-05-05 04:44:00 |
| 285 | PermissionDenied | Access denied for user root | worker | ERROR | 36 | 2026-06-06 05:55:00 |
| 322 | RateLimitExceeded | Too many requests from 10.0.0.1 | auth-svc | fatal | 43 | 2026-07-07 06:06:00 |
| 359 | ParseError | Malformed JSON in request body | payment-api | error | 50 | 2026-08-08 07:17:00 |
| 396 | nullpointerexception | Stack overflow in recursive call | user-svc | warning | 57 | 2026-09-09 08:28:00 |
| 433 | TYPEERROR | Division by zero | search-api | info | 64 | 2026-01-10 09:39:00 |
| 470 | NullPointerException | Unexpected null value at line 42 | gateway | Fatal | NULL | 2026-02-11 10:50:00 |
Expected output
| err_id | err_type | message | svc_name | severity | count | first_at |
|---|---|---|---|---|---|---|
| 914 | ConnectionTimeout | Connection timed out after 30s | gateway | Fatal | 155 | 2026-05-23 22:02:00 |
| 2024 | ConnectionTimeout | Connection timed out after 30s | gateway | Fatal | 365 | 2026-08-25 04:32:00 |
| 3134 | ConnectionTimeout | Connection timed out after 30s | gateway | Fatal | 575 | 2026-02-27 10:02:00 |
Worked solution and explanation
What this is really asking
This is a containment search wearing an incident costume. The word 'contains' is the whole problem: 'timed out' never stands alone in message, it sits inside 'Connection timed out after 30s', so any predicate that compares the whole cell finds nothing. Anyone can write the WHERE; what separates candidates is reading 'contains' as a substring with wildcards on both sides. Write message = 'timed out' or an anchored LIKE 'timed out%' and the query returns zero rows in the middle of an incident, and the on-call concludes the gateway has no timeout problem.
Break down the requirements
Step 1: Match the service as a substring of svc_name
svc_name LIKE '%gateway%' reads the requirement literally: 'gateway' anywhere in the name. On err_tracks the service is stored as exactly 'gateway', so today the pattern and an equality agree. The wildcards are what the prompt asked for, and they keep the query right the day a 'gateway-eu' starts logging.
Step 2: Find the phrase inside message
The only timeout text in the table is 'Connection timed out after 30s'. The phrase starts after 'Connection ', so message = 'timed out' and LIKE 'timed out%' both miss it. Only LIKE '%timed out%' finds it wherever it sits.
Step 3: AND, not OR
Both conditions must hold on the same row. Timeouts also land on 'user-svc' and the other services, and 'gateway' logs plenty of non-timeout errors such as 'File /tmp/data.csv not found'. With OR you would return all of them.
The solution
SELECT
err_id,
err_type,
message,
svc_name,
severity,
count,
first_at
FROM err_tracks
WHERE svc_name LIKE '%gateway%'
AND message LIKE '%timed out%'Common follow-up questions
Common follow-up questions
- What if service names started arriving as 'Gateway' or 'GATEWAY'? (Tests whether you normalize case (`ILIKE`, or lowercasing both sides) instead of trusting today's data.)
- Also match the single word 'timeout' (no space). (Pushes to a regular expression such as `REGEXP_LIKE(message, 'timed ?out')`, or two `LIKE` clauses joined with `OR` inside parentheses.)
- Make this query fast on 20M rows. (Opens trigram indexes, partition pruning on `first_at`, or moving log search to a search engine.)