Average Process Duration per Machine from Start and End Events

Quick Overview

Write a PostgreSQL query that pairs each process's start and end events from a machine activity log and reports the average process duration per machine, rounded to three decimals. It tests self-joins on a composite key, grouped averages and exact decimal rounding.

Average Process Duration per Machine from Start and End Events

Company: Waymo

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Each process on a machine logs two rows in `activity`: one when it starts and one when it ends. For each machine, return `machine_id` and `processing_time`: the total of (`end` timestamp minus `start` timestamp) over its processes, divided by the number of processes, rounded to 3 decimal places. ### Input Table(s) Table `activity` (primary key `(machine_id, process_id, activity_type)`): | Column | Type | Meaning | |---|---|---| | machine_id | INT | Machine identifier | | process_id | INT | Process identifier, numbered within its machine | | activity_type | VARCHAR | Either `'start'` or `'end'` | | timestamp | FLOAT | Time of the event in seconds (double precision) | ### Output Contract - Columns, in this order: `machine_id`, `processing_time`. - One row per machine. - `processing_time` is the mean process duration on that machine, where a duration is the `end` timestamp minus the `start` timestamp of the same `(machine_id, process_id)`. Round it to 3 decimal places. - Order by `machine_id` ascending. ### Example `activity` | machine_id | process_id | activity_type | timestamp | |---|---|---|---| | 1 | 0 | start | 1.500 | | 1 | 0 | end | 3.000 | | 1 | 1 | start | 4.250 | | 1 | 1 | end | 5.000 | | 1 | 2 | start | 6.000 | | 1 | 2 | end | 7.000 | | 2 | 0 | start | 0.100 | | 2 | 0 | end | 2.300 | | 2 | 1 | start | 10.000 | | 2 | 1 | end | 10.456 | | 2 | 2 | start | 12.400 | | 2 | 2 | end | 12.500 | Expected result: | machine_id | processing_time | |---|---| | 1 | 1.083 | | 2 | 0.919 | Machine 1's processes take 1.5, 0.75 and 1.0 seconds, so the average is 3.25 / 3 = 1.08333, which rounds to 1.083. Machine 2's processes take 2.2, 0.456 and 0.1 seconds, so the average is 2.756 / 3 = 0.91867, which rounds to 0.919. ### Constraints and Clarifications - `activity` has between 1 and 10,000 rows. - Every `(machine_id, process_id)` pair has exactly one `'start'` row and one `'end'` row, and the start timestamp is strictly earlier than the end timestamp. No timestamp is NULL. - `process_id` is numbered per machine: process 0 on machine 1 and process 0 on machine 2 are different processes. - Every machine runs the same number of processes. - Read each timestamp as the decimal value shown in the data (seconds). Compute in exact decimal arithmetic and round only the final average, with halves rounded away from zero. ```hint Identifying a process A process is identified by two columns, not one, and its duration needs values from two different rows. ```

Overview: Write a PostgreSQL query that pairs each process's start and end events from a machine activity log and reports the average process duration per machine, rounded to three decimals. It tests self-joins on a composite key, grouped averages and exact decimal rounding.

Waymo logo
Waymo
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Each process on a machine logs two rows in activity: one when it starts and one when it ends. For each machine, return machine_id and processing_time: the total of (end timestamp minus start timestamp) over its processes, divided by the number of processes, rounded to 3 decimal places.

Input Table(s)

Table activity (primary key (machine_id, process_id, activity_type)):

ColumnTypeMeaning
machine_idINTMachine identifier
process_idINTProcess identifier, numbered within its machine
activity_typeVARCHAREither 'start' or 'end'
timestampFLOATTime of the event in seconds (double precision)

Output Contract

  • Columns, in this order: machine_id , processing_time .
  • One row per machine.
  • processing_time is the mean process duration on that machine, where a duration is the end timestamp minus the start timestamp of the same (machine_id, process_id) . Round it to 3 decimal places.
  • Order by machine_id ascending.

Example

activity

machine_idprocess_idactivity_typetimestamp
10start1.500
10end3.000
11start4.250
11end5.000
12start6.000
12end7.000
20start0.100
20end2.300
21start10.000
21end10.456
22start12.400
22end12.500

Expected result:

machine_idprocessing_time
11.083
20.919

Machine 1's processes take 1.5, 0.75 and 1.0 seconds, so the average is 3.25 / 3 = 1.08333, which rounds to 1.083. Machine 2's processes take 2.2, 0.456 and 0.1 seconds, so the average is 2.756 / 3 = 0.91867, which rounds to 0.919.

Constraints and Clarifications

  • activity has between 1 and 10,000 rows.
  • Every (machine_id, process_id) pair has exactly one 'start' row and one 'end' row, and the start timestamp is strictly earlier than the end timestamp. No timestamp is NULL.
  • process_id is numbered per machine: process 0 on machine 1 and process 0 on machine 2 are different processes.
  • Every machine runs the same number of processes.
  • Read each timestamp as the decimal value shown in the data (seconds). Compute in exact decimal arithmetic and round only the final average, with halves rounded away from zero.