As reported: ServiceNow Software Engineer (IC2) loop, round 1 (DSA), Nov 2026: find all zones, then — as the follow-up — all trapped zones.
There are n cities 0…n−1 and one-way connections [a, b] (you can travel from a to b). A zone is a maximal group of cities in which every city can reach every other city (a strongly connected component; a city on its own can be a zone). A trapped zone is a zone that has at least one incoming connection from another zone and no outgoing connection to any other zone — once you are in, you cannot leave.
Implement:
zones(n, connections)— all zones.trappedZones(n, connections)— all trapped zones (the follow-up).
Cities inside a zone and the zones themselves may be returned in any order.
Example
n = 8
connections = [[0,1],[1,2],[2,3],[3,0],[3,4],[4,5],[5,4],[6,5],[6,7]]
zones -> [[0,1,2,3],[4,5],[6],[7]]
trappedZones -> [[4,5],[7]]
[6] is not trapped (it has outgoing connections), and [0,1,2,3] can leave to 4.
Constraints: 1 ≤ n ≤ 10^4; 0 ≤ connections.length ≤ 2 · 10^4; a ≠ b; a connection may repeat.
Code runner format: line 1 n, line 2 connections. The harness calls both functions and prints two lines: the zones, then the trapped zones (each zone sorted, zones sorted).