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

batch_jobs: 10 of 200 rows
job_idjob_namestatusrows_donestartedendedpriorityretries
1047user_exportrunning972026-02-02 01:00:002026-02-02 03:11:0030
1094data_cleanupfailed1942026-03-03 02:00:002026-03-03 04:22:0060
1141email_blastqueued2912026-04-04 03:00:002026-04-04 05:33:0090
1188index_rebuildcanceled3882026-05-05 04:00:00NULL20
1235cache_warmCompleted4852026-06-06 05:00:002026-06-06 07:55:0050
1282log_archiveFAILEDNULL2026-07-07 06:00:002026-07-07 08:06:0080
1329metric_aggcompleted6792026-08-08 07:00:002026-08-08 09:17:0010
1376backup_dbrunning7762026-09-09 08:00:00NULL40
1423sync_elasticfailed8732026-01-10 09:00:002026-01-10 11:39:0075
1470daily_reportqueued9702026-02-11 10:00:002026-02-11 12:50:0000

Expected output

First 5 of 134 rows
job_idjob_nameminutes_elapsed
1047user_export0
1094data_cleanup0
1141email_blast0
1235cache_warm0
1282log_archive0

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

Finished jobs with their runtime in minutes
SELECT job_id, job_name, (ended - started) / 60 AS minutes_elapsed
FROM batch_jobs
WHERE ended IS NOT NULL AND ended > started

Common follow-up questions