Against the Clock
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
- L5
- Asked in
- Instacart
Problem
Our batch scheduler stores each job's start and end time as Unix epoch seconds, writing the end time only once a job finishes, so jobs still running or killed mid-flight have no end time. For the jobs that finished, report how many whole minutes each one ran, alongside its job_id and job_name.
Promised by noon. Delivered at midnight.
Tables
batch_jobs
Columns: job_id INTEGER, job_name TEXT, started TEXT, ended TEXT, status TEXT, rows_done INTEGER, priority INTEGER, retries INTEGER
| job_id | job_name | status | rows_done | started | ended | priority | retries |
|---|---|---|---|---|---|---|---|
| 1047 | user_export | running | 97 | 2026-02-02 01:00:00 | 2026-02-02 03:11:00 | 3 | 0 |
| 1094 | data_cleanup | failed | 194 | 2026-03-03 02:00:00 | 2026-03-03 04:22:00 | 6 | 0 |
| 1141 | email_blast | queued | 291 | 2026-04-04 03:00:00 | 2026-04-04 05:33:00 | 9 | 0 |
| 1188 | index_rebuild | canceled | 388 | 2026-05-05 04:00:00 | NULL | 2 | 0 |
| 1235 | cache_warm | Completed | 485 | 2026-06-06 05:00:00 | 2026-06-06 07:55:00 | 5 | 0 |
| 1282 | log_archive | FAILED | NULL | 2026-07-07 06:00:00 | 2026-07-07 08:06:00 | 8 | 0 |
| 1329 | metric_agg | completed | 679 | 2026-08-08 07:00:00 | 2026-08-08 09:17:00 | 1 | 0 |
| 1376 | backup_db | running | 776 | 2026-09-09 08:00:00 | NULL | 4 | 0 |
| 1423 | sync_elastic | failed | 873 | 2026-01-10 09:00:00 | 2026-01-10 11:39:00 | 7 | 5 |
| 1470 | daily_report | queued | 970 | 2026-02-11 10:00:00 | 2026-02-11 12:50:00 | 0 | 0 |
Expected output
| job_id | job_name | minutes_elapsed |
|---|---|---|
| 1047 | user_export | 0 |
| 1094 | data_cleanup | 0 |
| 1141 | email_blast | 0 |
| 1235 | cache_warm | 0 |
| 1282 | log_archive | 0 |
Worked solution and explanation
What this really tests
This is a duration calculation dressed up as job monitoring. The real skill: computing the gap between two timestamps while correctly dropping the records that have no end time yet. Anyone can subtract two numbers. The trap is the jobs still running or killed mid-flight: their ended value is NULL, and subtracting against NULL yields NULL, so if you compute on them you either pad the result with empty rows or silently invert the question, reporting phantom runtimes for work that never finished.
Why the finished-only scope is load-bearing
About 10 percent of these rows have no ended value at any moment, because a running job has not finished and a canceled one never will. Any arithmetic against a NULL returns NULL, so those rows would surface as blank durations. The business question is only about jobs that completed, so the scope has to be enforced before the arithmetic runs, not cleaned up afterward.
Step 1: Keep only the jobs that finished
Restrict to rows where ended is present. Comparing ended > started on top of that also guards against clock skew or reversed timestamps that would produce a negative runtime. Rows still running fall away here, which is exactly the intent.
Step 2: Turn the second gap into minutes
Both columns are Unix epoch seconds, so ended - started is the elapsed time in seconds. Integer-divide by 60 to get whole minutes, which truncates the leftover seconds so a run of 2 hours 11 minutes reports as 131.
The solution
SELECT job_id, job_name, (ended - started) / 60 AS minutes_elapsed
FROM batch_jobs
WHERE ended IS NOT NULL AND ended > startedCommon follow-up questions
- If every job in the table were still running, what would this query return, and is that the behavior you want? (Tests whether the candidate reasons about the empty result rather than assuming rows.)
- How would you report the average runtime per `job_name`, and how do the unfinished jobs affect that average? (Tests extending the metric into an aggregate over the finished-only scope.)
- How would you return only the jobs that ran longer than 150 minutes, and where in the query does that predicate belong? (Tests turning the duration into a threshold filter, the natural next ask.)