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
Coding & Algorithms
Software Engineer
Uber
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

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
offline processing
reverse union find
dynamic connectivity
sorting queries
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: n nodes, roads list of (u, v), closures list of road indices (distinct), queries list of (k, a, b) with 0 <= 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 ...


Submit Your Answer
Markdown supported