Find unhealthy services by latency and error rate thresholds

Quick Overview

Given a list of services with latency and error rate readings, return the IDs of the services that exceed either threshold, sorted ascending. It tests careful condition checks: strict comparisons, combining two conditions with or, and listing each service only once.

Find unhealthy services by latency and error rate thresholds

Company: HashiCorp

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a list of services, where each service is represented as a list `[id, latency, error_rate]`. You are also given a `latency_threshold` and an `error_rate_threshold`. A service is considered **unhealthy** if its `latency` is strictly greater than `latency_threshold`, or its `error_rate` is strictly greater than `error_rate_threshold`. Either condition alone is enough. Return the IDs of the unhealthy services in ascending order. ### Function Signature ```python def unhealthy_services(services: list[list[float]], latency_threshold: float, error_rate_threshold: float) -> list[int]: ``` ### Rules - Each service is `[id, latency, error_rate]`. The `id` and `latency` are whole numbers; `error_rate` may have a fractional part, such as `0.5`. - Both comparisons are strict. A value equal to its threshold does not make a service unhealthy. - A service that exceeds both thresholds appears once in the result. - Return the IDs as integers sorted in ascending order. Return an empty list when every service is healthy. ### Constraints - `1 <= len(services) <= 10^4` - `0 <= id <= 10^5` - `0 <= latency <= 10^6` - `0.0 <= error_rate <= 100.0` - `0 <= latency_threshold <= 10^6` - `0.0 <= error_rate_threshold <= 100.0` - All service IDs are unique. ### Examples ```text Input: services = [[1, 200, 5], [2, 500, 1], [3, 100, 12], [4, 50, 0.5]], latency_threshold = 300, error_rate_threshold = 10 Output: [2, 3] ``` Service 2 has latency 500, which is above 300. Service 3 has error rate 12, which is above 10. Services 1 and 4 are within both thresholds. ```text Input: services = [[10, 100, 2], [20, 150, 3], [30, 200, 4]], latency_threshold = 200, error_rate_threshold = 5 Output: [] ``` Service 30 has latency exactly 200, which is not strictly greater than the threshold, and no error rate is above 5. Every service is healthy, so the result is empty. ```text Input: services = [[7, 300, 10.0], [9, 450, 25.5], [3, 301, 0.0], [5, 10, 10.5]], latency_threshold = 300, error_rate_threshold = 10 Output: [3, 5, 9] ``` Service 7 sits exactly on both thresholds, so it is healthy. Service 9 exceeds both and is listed once, service 3 exceeds the latency threshold and service 5 exceeds the error rate threshold. The IDs are sorted ascending even though the input lists 9 first.

Overview: Given a list of services with latency and error rate readings, return the IDs of the services that exceed either threshold, sorted ascending. It tests careful condition checks: strict comparisons, combining two conditions with or, and listing each service only once.

HashiCorp logo
HashiCorp
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a list of services, where each service is represented as a list [id, latency, error_rate]. You are also given a latency_threshold and an error_rate_threshold.

A service is considered unhealthy if its latency is strictly greater than latency_threshold, or its error_rate is strictly greater than error_rate_threshold. Either condition alone is enough.

Return the IDs of the unhealthy services in ascending order.

Function Signature

def unhealthy_services(services: list[list[float]], latency_threshold: float, error_rate_threshold: float) -> list[int]:

Rules

  • Each service is [id, latency, error_rate] . The id and latency are whole numbers; error_rate may have a fractional part, such as 0.5 .
  • Both comparisons are strict. A value equal to its threshold does not make a service unhealthy.
  • A service that exceeds both thresholds appears once in the result.
  • Return the IDs as integers sorted in ascending order. Return an empty list when every service is healthy.

Constraints

  • 1 <= len(services) <= 10^4
  • 0 <= id <= 10^5
  • 0 <= latency <= 10^6
  • 0.0 <= error_rate <= 100.0
  • 0 <= latency_threshold <= 10^6
  • 0.0 <= error_rate_threshold <= 100.0
  • All service IDs are unique.

Examples

Input: services = [[1, 200, 5], [2, 500, 1], [3, 100, 12], [4, 50, 0.5]], latency_threshold = 300, error_rate_threshold = 10
Output: [2, 3]

Service 2 has latency 500, which is above 300. Service 3 has error rate 12, which is above 10. Services 1 and 4 are within both thresholds.

Input: services = [[10, 100, 2], [20, 150, 3], [30, 200, 4]], latency_threshold = 200, error_rate_threshold = 5
Output: []

Service 30 has latency exactly 200, which is not strictly greater than the threshold, and no error rate is above 5. Every service is healthy, so the result is empty.

Input: services = [[7, 300, 10.0], [9, 450, 25.5], [3, 301, 0.0], [5, 10, 10.5]], latency_threshold = 300, error_rate_threshold = 10
Output: [3, 5, 9]

Service 7 sits exactly on both thresholds, so it is healthy. Service 9 exceeds both and is listed once, service 3 exceeds the latency threshold and service 5 exceeds the error rate threshold. The IDs are sorted ascending even though the input lists 9 first.