Answer Connectivity Queries After a Sequence of Road Closures
Last updated: November 29, 2026
Quick Overview
Given a road network, an ordered list of road closures, and queries of the form (k, a, b) asking whether a and b are still connected after the first k closures, answer all queries efficiently.
Uber
November 29, 20260
0
0 solved
Given a road network, an ordered list of road closures, and queries of the form (k, a, b) asking whether a and b are still connected after the first k closures, answer all queries efficiently.
During marathons, storms or protests, Uber's routing must know which parts of a city remain reachable as closures accumulate. Union find cannot delete edges, so the onsite interviewer is checking whether you process the timeline in reverse, turning closures into additions.
What the Interviewer Expects
- Notice union find supports only additions
- Process closures in reverse, adding roads back
- Start from the graph with every scheduled closure applied
- Sort queries by k descending and answer at the right moment
- Handle roads closed more than once or never present
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What if closures and reopenings interleave arbitrarily?
- How would you answer 'how many components' after each closure?
- What if queries must be answered online, as closures happen?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input:
nnodes,roadslist of(u, v),closureslist of road indices (distinct),querieslist of(k, a, b)with0 <= k <= len(closures). - Output: list of booleans in query order....
High-Level Design
After k closures, the open roads are all roads except closures[0..k-1]. Going backward from k = len(closures) to 0 only adds roads, which union find handles. Initialize the DSU with every road that ...