Compute continuous and discrete latency percentiles per API endpoint

Quick Overview

Write a PostgreSQL query that reports, for each API endpoint, the sample count and the 25th, 50th and 90th latency percentiles computed both by interpolation and as observed values. It tests NULL handling, single-sample edge cases and the difference between continuous and discrete percentiles.

Compute continuous and discrete latency percentiles per API endpoint

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

The `api_logs` table has one row per API request, with the endpoint that served it and the response latency in milliseconds. For each endpoint, compute the 25th, 50th and 90th latency percentiles in two ways. The continuous percentiles interpolate between neighboring observed values, as `PERCENTILE_CONT` does. The discrete percentiles always return an observed value, as `PERCENTILE_DISC` does. Also report how many samples each endpoint contributed. ```hint Watch the details Neither percentile function applies the one-sample rule for you. Also check which argument types PostgreSQL's two-argument `ROUND` accepts. ``` ### Input Table(s) Table `api_logs`, primary key `id`: | Column | Type | Meaning | |---|---|---| | `id` | INT | Primary key, one row per request | | `endpoint` | VARCHAR(100) | API path that served the request | | `latency_ms` | INT | Response time in milliseconds, may be NULL | ### Output Contract Return one row per endpoint with these columns, in this order: | Column | Meaning | |---|---| | `endpoint` | The endpoint | | `sample_count` | Number of rows for the endpoint with a non-NULL `latency_ms` | | `p25_cont` | Continuous 25th percentile, rounded to 2 decimals | | `median_cont` | Continuous 50th percentile, rounded to 2 decimals | | `p90_cont` | Continuous 90th percentile, rounded to 2 decimals | | `p25_disc` | Discrete 25th percentile | | `median_disc` | Discrete 50th percentile | | `p90_disc` | Discrete 90th percentile | - The three `*_cont` columns are NULL when the endpoint has exactly one non-NULL sample. - The three `*_disc` columns are always one of the endpoint's observed latencies, unrounded. - Order the rows by `endpoint` ascending. Each endpoint appears once, so this order is unique. For an endpoint with sorted non-NULL latencies $x_1 \le x_2 \le \dots \le x_n$ and a fraction $p$: - the continuous percentile uses the position $h = 1 + p(n - 1)$ and returns $x_{\lfloor h \rfloor} + (h - \lfloor h \rfloor)(x_{\lfloor h \rfloor + 1} - x_{\lfloor h \rfloor})$, which is simply $x_h$ when $h$ is a whole number; - the discrete percentile returns $x_k$ with $k = \lceil p \cdot n \rceil$, the first value whose cumulative share of the samples reaches $p$. These are the definitions PostgreSQL's `PERCENTILE_CONT` and `PERCENTILE_DISC` use. ### Example Input `api_logs`: | id | endpoint | latency_ms | |---|---|---| | 1 | /search | 50 | | 2 | /search | 90 | | 3 | /search | NULL | | 4 | /checkout | 10 | | 5 | /checkout | 10 | Expected output: | endpoint | sample_count | p25_cont | median_cont | p90_cont | p25_disc | median_disc | p90_disc | |---|---|---|---|---|---|---|---| | /checkout | 2 | 10.00 | 10.00 | 10.00 | 10 | 10 | 10 | | /search | 2 | 60.00 | 70.00 | 86.00 | 50 | 50 | 90 | /search keeps 50 and 90 once the NULL row is dropped, so `sample_count` is 2. For the 25th percentile $h = 1.25$, which gives 50 + 0.25 × 40 = 60. The median has $h = 1.5$ and comes out at 70, and the 90th percentile has $h = 1.9$ and comes out at 86. On the discrete side, $\lceil 0.25 \cdot 2 \rceil = 1$ and $\lceil 0.5 \cdot 2 \rceil = 1$ both pick 50, while $\lceil 0.9 \cdot 2 \rceil = 2$ picks 90. /checkout has two equal values, so every percentile is 10. ### Constraints and Clarifications - The table has 1 to 10,000 rows. - `latency_ms` is a non-negative integer or NULL. Rows with a NULL latency are excluded from every column, including `sample_count`. - `endpoint` is a non-empty string of at most 100 characters. - Every endpoint has at least one non-NULL `latency_ms`, so every endpoint in the table appears in the output. - Repeated latencies within an endpoint are separate samples and all count. Ties in the sort order do not change any result, because tied rows hold the same value. - An endpoint with a single sample gets NULL in the three `*_cont` columns and that sample in the three `*_disc` columns.

Overview: Write a PostgreSQL query that reports, for each API endpoint, the sample count and the 25th, 50th and 90th latency percentiles computed both by interpolation and as observed values. It tests NULL handling, single-sample edge cases and the difference between continuous and discrete percentiles.

Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

The api_logs table has one row per API request, with the endpoint that served it and the response latency in milliseconds. For each endpoint, compute the 25th, 50th and 90th latency percentiles in two ways. The continuous percentiles interpolate between neighboring observed values, as PERCENTILE_CONT does. The discrete percentiles always return an observed value, as PERCENTILE_DISC does. Also report how many samples each endpoint contributed.

Input Table(s)

Table api_logs, primary key id:

ColumnTypeMeaning
idINTPrimary key, one row per request
endpointVARCHAR(100)API path that served the request
latency_msINTResponse time in milliseconds, may be NULL

Output Contract

Return one row per endpoint with these columns, in this order:

ColumnMeaning
endpointThe endpoint
sample_countNumber of rows for the endpoint with a non-NULL latency_ms
p25_contContinuous 25th percentile, rounded to 2 decimals
median_contContinuous 50th percentile, rounded to 2 decimals
p90_contContinuous 90th percentile, rounded to 2 decimals
p25_discDiscrete 25th percentile
median_discDiscrete 50th percentile
p90_discDiscrete 90th percentile
  • The three *_cont columns are NULL when the endpoint has exactly one non-NULL sample.
  • The three *_disc columns are always one of the endpoint's observed latencies, unrounded.
  • Order the rows by endpoint ascending. Each endpoint appears once, so this order is unique.

For an endpoint with sorted non-NULL latencies x1≤x2≤⋯≤xnx_1 \le x_2 \le \dots \le x_n and a fraction pp:

  • the continuous percentile uses the position h=1+p(n−1)h = 1 + p(n - 1) and returns x⌊h⌋+(h−⌊h⌋)(x⌊h⌋+1−x⌊h⌋)x_{\lfloor h \rfloor} + (h - \lfloor h \rfloor)(x_{\lfloor h \rfloor + 1} - x_{\lfloor h \rfloor}) , which is simply xhx_h when hh is a whole number;
  • the discrete percentile returns xkx_k with k=⌈p⋅n⌉k = \lceil p \cdot n \rceil , the first value whose cumulative share of the samples reaches pp .

These are the definitions PostgreSQL's PERCENTILE_CONT and PERCENTILE_DISC use.

Example

Input api_logs:

idendpointlatency_ms
1/search50
2/search90
3/searchNULL
4/checkout10
5/checkout10

Expected output:

endpointsample_countp25_contmedian_contp90_contp25_discmedian_discp90_disc
/checkout210.0010.0010.00101010
/search260.0070.0086.00505090

/search keeps 50 and 90 once the NULL row is dropped, so sample_count is 2. For the 25th percentile h=1.25h = 1.25, which gives 50 + 0.25 × 40 = 60. The median has h=1.5h = 1.5 and comes out at 70, and the 90th percentile has h=1.9h = 1.9 and comes out at 86. On the discrete side, ⌈0.25⋅2⌉=1\lceil 0.25 \cdot 2 \rceil = 1 and ⌈0.5⋅2⌉=1\lceil 0.5 \cdot 2 \rceil = 1 both pick 50, while ⌈0.9⋅2⌉=2\lceil 0.9 \cdot 2 \rceil = 2 picks 90. /checkout has two equal values, so every percentile is 10.

Constraints and Clarifications

  • The table has 1 to 10,000 rows.
  • latency_ms is a non-negative integer or NULL. Rows with a NULL latency are excluded from every column, including sample_count .
  • endpoint is a non-empty string of at most 100 characters.
  • Every endpoint has at least one non-NULL latency_ms , so every endpoint in the table appears in the output.
  • Repeated latencies within an endpoint are separate samples and all count. Ties in the sort order do not change any result, because tied rows hold the same value.
  • An endpoint with a single sample gets NULL in the three *_cont columns and that sample in the three *_disc columns.