Mutual Station Connectivity Queries
Updated
Problem statement
A directed subway network has stations labeled from 0 through n - 1. Each row [from, to] in connections means trains can travel directly from from to to.
For each row [a, b] in queries, determine whether a can reach b and b can reach a through zero or more directed connections. A station is mutually reachable with itself.
Return the query answers in order.
Function
mutuallyConnected(n: int, connections: int[][], queries: int[][]) → boolean[]Examples
Example 1
n = 5connections = [[0,1],[1,2],[2,0],[2,3],[3,4],[4,3]]queries = [[0,2],[0,3],[3,4],[4,0],[1,1]]return = [true,false,true,false,true]Stations 0, 1, 2 form one strongly connected component, while 3, 4 form another. The edge from 2 toward 3 does not provide a path back.
Example 2
n = 4connections = [[0,1],[1,0],[2,3]]queries = [[0,1],[0,2],[2,3],[3,3]]return = [true,false,false,true]Only stations 0 and 1 are distinct mutually reachable stations. The one-way edge 2 → 3 is insufficient, and a station always reaches itself.
Example 3
n = 1connections = []queries = [[0,0]]return = [true]The only station is mutually reachable with itself through a zero-edge path.
Constraints
1 <= n <= 100000.0 <= connections.length <= 200000.1 <= queries.length <= 100000.- Every station index in
connectionsandqueriesis between0andn - 1. - Connections may contain self-loops or duplicate edges.
More Bloomberg LP Phone Screen questions
- Collatz Conjecture with Shared MemoizationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
- Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
- Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Sparse Vector and Matrix OperationsPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Spreadsheet With Dependent CellsCharacter.AI · PHONE SCREEN
- Evaluate Live Spreadsheet ExpressionsStrala · PHONE SCREEN
- Spreadsheet Formula EvaluatorPika · PHONE SCREEN
- Spreadsheet Formula DependenciesAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Periodic-Color Binary Forest RootsGoogle · PHONE SCREENGoogle interview questions
- Spreadsheet Formula EvaluatorHarvey · PHONE SCREEN · ONSITE INTERVIEWHarvey interview questions
- Find Trapped ZonesServiceNow · PHONE SCREENServiceNow interview questions
- Spreadsheet Formula DependenciesCitadel · PHONE SCREENCitadel interview questions
- Versioned Friend RecommendationsOpenAI · PHONE SCREENOpenAI interview questions
- Visit Desired Attractions Without Reusing a TrailDuolingo · PHONE SCREENDuolingo interview questions