Skip to main content
All Questions

Find all zones, the maximal groups of mutually reachable cities, in a directed graph.

Unlock detailed company stats for this questionUpgrade

connections[i] = [a, b] is a one-way connection from city a to city b. A zone is a maximal group of cities where for any two cities u and v in the zone there is a directed path from u to v and from v to u. Test case: connections = [(0,1),(1,2),(2,3),(3,0),(3,4),(4,5),(5,4),(6,5),(6,7)], n = 8 -> [[0,1,2,3],[4,5],[6],[7]]. Follow-up: a trapped zone has at least one incoming connection from another zone and no outgoing connections to any other zone; once entered, you can travel within it but cannot leave. Same input -> [[4,5],[7]].

A trapped zone is a zone that has at least one incoming connection from another zone and no outgoing connections to any other zone. Follow-up: find all the trapped zones. Follow-up test case: connections = [(0,1),(1,2),(2,3),(3,0),(3,4),(4,5),(5,4),(6,5),(6,7)] -> [[4, 5], [7]]

Expect follow-ups like

  • Find all trapped zones: zones with at least one incoming connection from another zone and no outgoing connections to any other zone.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.