Find A Threshold-Limited Path With Minimum Required Safety
GoogleSoftware Engineer · Technical ScreenLast updated
Find the minimum security clearance needed to travel between two nodes in a graph where edges exceeding a given latency threshold cannot be used.
Problem Statement
You are designing a routing engine for a secure communications network with n servers numbered from 0 to n - 1. The network is represented as an undirected graph with bidirectional connections given by a 2D array edges, where edges[i] = [u, v, latency, safety_level] indicates a link between server u and server v with transmission delay latency and required safety clearance safety_level.
You are also given two server IDs, source and destination, and a threshold value max_latency.
Rules for valid paths:
- A link can only be used if
latency <= max_latency. - The safety clearance required for an entire path is defined as the maximum
safety_levelamong all links traversed on that path.
Return the minimum required safety clearance to travel from source to destination. If source == destination, return 0. If no valid path exists, return -1.
Function Signature
def min_required_safety(n: int, edges: list[list[int]], source: int, destination: int, max_latency: int) -> int:
pass
Input and Output Format
- Input:
n: integer representing the number of nodes (0ton - 1).edges: list of[u, v, latency, safety_level]integers.source: starting node integer.destination: target node integer.max_latency: integer upper bound on an edge's latency.
- Output:
- An integer representing the minimum required clearance, or
-1if unreachable.
- An integer representing the minimum required clearance, or
Examples
Example 1
Input:
n = 4
edges = [
[0, 1, 10, 5],
[1, 2, 8, 9],
[0, 2, 15, 7],
[2, 3, 5, 4]
]
source = 0
destination = 3
max_latency = 12
Output: 9
Explanation: The link (0, 2) has latency = 15 > 12, so it cannot be used. The only available path is 0 -> 1 -> 2 -> 3. The safety levels along this path are 5, 9, and 4. The path clearance requirement is max(5, 9, 4) = 9.
Example 2
Input:
n = 3
edges = [
[0, 1, 5, 8],
[1, 2, 6, 7]
]
source = 0
destination = 2
max_latency = 4
Output: -1
Explanation: All available edges have latency greater than max_latency = 4. No path exists, so return -1.
Constraints
1 <= n <= 10^50 <= len(edges) <= 2 * 10^5edges[i] = [u, v, latency, safety_level]where0 <= u, v < nandu != v0 <= latency, safety_level, max_latency <= 10^90 <= source, destination < n- There can be multiple edges between the same pair of vertices.
Hints
Hint 1
Discard or ignore any edge whose latency exceeds max_latency before or during path finding.
Hint 2
This is a minimax path problem: instead of minimizing the sum of weights, adapt Dijkstra's algorithm so the distance metric tracks the maximum safety level encountered so far.
Hint 3
Handle the base case where source == destination by immediately returning 0, and return -1 if the destination remains unvisited after exhausting the priority queue.
Follow-up questions
- How does the Dijkstra-based approach compare in time and space complexity to binary searching over the safety levels combined with BFS?
- If you were asked to answer thousands of (source, destination, max_latency) queries offline, how could you optimize the solution using Disjoint Set Union (DSU)?
- What changes would be required if the threshold applied to the cumulative latency of the entire path rather than individual edge latencies?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.