Flag database queries slower than twice their fingerprint's average time

Quick Overview

Group database queries by fingerprint, find the ones whose execution time is more than twice their group's average, and return them grouped and sorted by fingerprint and then by time. It tests hash map grouping, an exact threshold comparison and multi-level output ordering.

Flag database queries slower than twice their fingerprint's average time

Company: Discord

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a list of database queries, where each query is a pair `[fingerprint, execution_time]`. The `fingerprint` is a string that identifies the type or pattern of the query (for example `"SELECT_users"` or `"INSERT_orders"`). The `execution_time` is a positive integer number of milliseconds. Group the queries by their fingerprint. For each fingerprint group, compute the average execution time. A query is an outlier if its execution time is strictly greater than 2 times the average execution time of its fingerprint group. Return a list of lists, where each inner list holds the `[fingerprint, execution_time]` pairs that are outliers for one fingerprint. Sort the groups alphabetically by fingerprint, and within each group sort the outliers by execution time in descending order. If a fingerprint group has no outliers, leave it out of the result. ### Function Signature ```python def find_outlier_queries(queries: list[list]) -> list[list[list]]: ``` ### Rules - Each element of `queries` is a two-element list `[fingerprint, execution_time]`: a string, then an integer. - Fingerprints are case-sensitive, so `"SELECT_users"` and `"select_users"` are different groups. - Alphabetical order means plain string comparison by character code, the order Python's `sorted` produces. Digits sort before uppercase letters, uppercase letters before `_`, and `_` before lowercase letters. - The average is the exact mean of the group, with no rounding. The comparison is strict: a query whose time equals twice the average is not an outlier. - Inside a group, outliers go from the largest execution time to the smallest. If two outlier queries have the same time, both appear. - Return `[]` when no query is an outlier. ### Constraints - `1 <= len(queries) <= 10^4` - Each query is `[fingerprint, execution_time]` - `fingerprint` is a non-empty string of alphanumeric characters and underscores - `1 <= execution_time <= 10^6` ### Examples ```text Input: queries = [["SELECT_users",100],["SELECT_users",120],["SELECT_users",800],["INSERT_orders",50],["INSERT_orders",60],["INSERT_orders",300]] Output: [[["INSERT_orders",300]],[["SELECT_users",800]]] ``` For `INSERT_orders` the average is (50 + 60 + 300) / 3 ≈ 136.67, so the threshold is about 273.33 and only 300 is above it; for `SELECT_users` the average is (100 + 120 + 800) / 3 = 340, the threshold is 680, and only 800 is above it. The groups are sorted alphabetically, so `INSERT_orders` comes first. ```text Input: queries = [["GET_data",100],["GET_data",110],["GET_data",105],["POST_data",200],["POST_data",210]] Output: [] ``` For `GET_data` the average is 105 and the threshold is 210; for `POST_data` the average is 205 and the threshold is 410. No query exceeds its group's threshold, so the result is an empty list. ```text Input: queries = [["UPDATE_cache",10],["DELETE_logs",5],["UPDATE_cache",90],["UPDATE_cache",10],["DELETE_logs",40],["UPDATE_cache",100],["UPDATE_cache",10],["DELETE_logs",5],["UPDATE_cache",10]] Output: [[["DELETE_logs",40]],[["UPDATE_cache",100],["UPDATE_cache",90]]] ``` For `DELETE_logs` the average is 50 / 3 ≈ 16.67, so the threshold is about 33.33 and 40 is the only outlier. For `UPDATE_cache` the average is 230 / 6 ≈ 38.33, the threshold is about 76.67, and both 100 and 90 are outliers, listed largest first.

Overview: Group database queries by fingerprint, find the ones whose execution time is more than twice their group's average, and return them grouped and sorted by fingerprint and then by time. It tests hash map grouping, an exact threshold comparison and multi-level output ordering.

Discord logo
Discord
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a list of database queries, where each query is a pair [fingerprint, execution_time]. The fingerprint is a string that identifies the type or pattern of the query (for example "SELECT_users" or "INSERT_orders"). The execution_time is a positive integer number of milliseconds.

Group the queries by their fingerprint. For each fingerprint group, compute the average execution time. A query is an outlier if its execution time is strictly greater than 2 times the average execution time of its fingerprint group.

Return a list of lists, where each inner list holds the [fingerprint, execution_time] pairs that are outliers for one fingerprint. Sort the groups alphabetically by fingerprint, and within each group sort the outliers by execution time in descending order. If a fingerprint group has no outliers, leave it out of the result.

Function Signature

def find_outlier_queries(queries: list[list]) -> list[list[list]]:

Rules

  • Each element of queries is a two-element list [fingerprint, execution_time] : a string, then an integer.
  • Fingerprints are case-sensitive, so "SELECT_users" and "select_users" are different groups.
  • Alphabetical order means plain string comparison by character code, the order Python's sorted produces. Digits sort before uppercase letters, uppercase letters before _ , and _ before lowercase letters.
  • The average is the exact mean of the group, with no rounding. The comparison is strict: a query whose time equals twice the average is not an outlier.
  • Inside a group, outliers go from the largest execution time to the smallest. If two outlier queries have the same time, both appear.
  • Return [] when no query is an outlier.

Constraints

  • 1 <= len(queries) <= 10^4
  • Each query is [fingerprint, execution_time]
  • fingerprint is a non-empty string of alphanumeric characters and underscores
  • 1 <= execution_time <= 10^6

Examples

Input: queries = [["SELECT_users",100],["SELECT_users",120],["SELECT_users",800],["INSERT_orders",50],["INSERT_orders",60],["INSERT_orders",300]]
Output: [[["INSERT_orders",300]],[["SELECT_users",800]]]

For INSERT_orders the average is (50 + 60 + 300) / 3 ≈ 136.67, so the threshold is about 273.33 and only 300 is above it; for SELECT_users the average is (100 + 120 + 800) / 3 = 340, the threshold is 680, and only 800 is above it. The groups are sorted alphabetically, so INSERT_orders comes first.

Input: queries = [["GET_data",100],["GET_data",110],["GET_data",105],["POST_data",200],["POST_data",210]]
Output: []

For GET_data the average is 105 and the threshold is 210; for POST_data the average is 205 and the threshold is 410. No query exceeds its group's threshold, so the result is an empty list.

Input: queries = [["UPDATE_cache",10],["DELETE_logs",5],["UPDATE_cache",90],["UPDATE_cache",10],["DELETE_logs",40],["UPDATE_cache",100],["UPDATE_cache",10],["DELETE_logs",5],["UPDATE_cache",10]]
Output: [[["DELETE_logs",40]],[["UPDATE_cache",100],["UPDATE_cache",90]]]

For DELETE_logs the average is 50 / 3 ≈ 16.67, so the threshold is about 33.33 and 40 is the only outlier. For UPDATE_cache the average is 230 / 6 ≈ 38.33, the threshold is about 76.67, and both 100 and 90 are outliers, listed largest first.