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.

Each process on a machine logs two rows in the `activity` table: 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. Table `activity` (primary key `(machine_id, process_id, activity_type)`): - `machine_id` (INTEGER): machine identifier - `process_id` (INTEGER): process identifier, numbered within its machine - `activity_type` (VARCHAR): either `'start'` or `'end'` - `timestamp` (DOUBLE PRECISION): time of the event in seconds 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. 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.

Tables

activity(machine_id INTEGER, process_id INTEGER, activity_type VARCHAR(10), timestamp DOUBLE PRECISION)

Hints

  1. A process is identified by two columns, not one, and its duration needs values from two different rows.
  2. The timestamps are stored as double precision. Treat each one as the decimal value shown in the data, and round only the final average to 3 places, with halves rounded away from zero.
WaymoData ScientisteasyData Manipulation (SQL/Python)

Average Processing Time per Machine

Each process on a machine logs two rows in the activity table: 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.

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

  • machine_id (INTEGER): machine identifier
  • process_id (INTEGER): process identifier, numbered within its machine
  • activity_type (VARCHAR): either 'start' or 'end'
  • timestamp (DOUBLE PRECISION): time of the event in seconds

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.

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.

Expected Output

Tables

activity
activity
ColumnType
machine_idINTEGER
process_idINTEGER
activity_typeVARCHAR(10)
timestampDOUBLE PRECISION

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions