Delivery Service
Updated
Problem statement
A logistics network spans n cities numbered 1 through n. Each day has a morning shift and an afternoon shift.
The network uses 2 * n state nodes:
- Nodes
1throughnare the morning states of cities1throughn. - Nodes
n + 1through2 * nare the afternoon states. Nodei + nis the afternoon state of cityi.
The array routes lists scheduled directed courier routes. Each entry [u, v] means that a package can travel from state node u to state node v.
Within every city i, a package can always transition from its morning state i to its afternoon state i + n. It can never transition automatically from afternoon back to morning. These implicit morning-to-afternoon transitions are not included in routes.
A delivery from city u to city v is possible if a package starting at the morning state of city u can reach either the morning state or the afternoon state of city v by following scheduled routes and implicit transitions.
Return the number of ordered city pairs (u, v) for which delivery is impossible.
Function
countImpossibleDeliveries(n: int, routes: int[][]) → longExamples
Example 1
n = 2routes = []return = 2Each city can reach only itself through its implicit morning-to-afternoon transition. The impossible ordered pairs are (1, 2) and (2, 1).
Example 2
n = 3routes = [[1,2],[2,3]]return = 3City 1 can deliver to every city, city 2 cannot deliver to city 1, and city 3 cannot deliver to cities 1 or 2. Therefore, 3 ordered pairs are impossible.
Example 3
n = 2routes = [[3,2]]return = 1Node 3 is the afternoon state of city 1. City 1 reaches node 3 through its implicit transition and then reaches the morning state of city 2 through the scheduled route. City 2 still cannot deliver to city 1, so exactly one ordered pair is impossible.
Constraints
1 ≤ n ≤ 50,0000 ≤ routes.length ≤ 100,000routes[i].length = 21 ≤ routes[i][0], routes[i][1] ≤ 2 * n
More InMobi Online Assessment questions
- Validate a Bracket SequenceOA · Seen Oct 2026
- Count Substrings Matching with One Adjacent SwapOA · Seen Oct 2026
- Maximum Subarray with Start and End WindowsOA · Seen Oct 2026
- Minimum Service Cold StartsOA · Seen Oct 2026
- Range Maximum Subarray Sum QueriesOA · Seen Oct 2026
- Connected Components with Divisor ThresholdOA · Seen Aug 2026
- Maximize Impression ValueOA · Seen Aug 2026
- Supply Path OptimizationOA · Seen Aug 2026
- Shared InterestOA · Seen Jun 2026
- Temporal AnomaliesOA · Seen Jun 2026
Similar questions at other companies
- Detect a Cycle in Directed Hate RelationshipsArcesium · OAArcesium interview questions
- Can You Finish All Courses?Intuit · OAIntuit interview questions
- Module Rebuild CostsAirbnb · OAAirbnb interview questions
- Visit Desired Attractions Without Reusing a TrailDuolingo · PHONE SCREENDuolingo interview questions
- Course Order and CycleAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Deepest Common Ancestors in a Multi-Parent DAGSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Least-Strong Common DefeaterConfluent · PHONE SCREENConfluent interview questions
- Course ScheduleOracle · ONSITE INTERVIEWOracle interview questions
- Package Install OrderVerily · ONSITE INTERVIEW
- Task Build Order and File-Change RerunsVanta · PHONE SCREEN