DAG Downstream Nodes
Updated
Problem statement
A directed acyclic graph is encoded by adjacency rows. The first value in each row is a node and the remaining values are its direct downstream nodes.
Each query is either DIRECT node or ALL node. A direct query returns that node's direct downstream nodes. An all query returns every distinct node reachable by one or more edges. Sort every answer lexicographically and format it as [node1,node2]; return [] when the answer is empty.
Return one formatted answer for every query in order.
Function
queryDownstream(adjacency: String[][], queries: String[]) → String[]Examples
Example 1
adjacency = [["A","B","C"],["B","D"],["C","D","E"],["D"],["E"]]queries = ["DIRECT A","ALL A","DIRECT D","ALL C"]return = ["[B,C]","[B,C,D,E]","[]","[D,E]"]A directly reaches B and C. Its transitive result includes those nodes plus D and E once, even though D has two incoming paths.
Constraints
1 <= adjacency.length <= 100000.- Every node has exactly one adjacency row, including nodes with no outgoing edges.
- Node IDs are unique non-empty ASCII strings containing neither a comma nor whitespace.
- The graph has no self-edge, duplicate edge, or directed cycle.
1 <= queries.length <= 100000, and every queried node exists.- The total number of nodes visited across all
ALLqueries is at most300000.
More Notion interview questions
- Nested Todo ListPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Top Error Types in a Time WindowONSITE INTERVIEW · Seen Jun 2026
- Mutable Table AggregationsPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2026
- Page Permission InheritancePHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2026
- Text Document HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2026
- Sliding Window MedianONSITE INTERVIEW · Seen Oct 2025
- Task Row ModificationsONSITE INTERVIEW · Seen Oct 2025
- JSON Block Tree OperationsONSITE INTERVIEW · Seen Jul 2025
- Block Type with the Most ChildrenPHONE SCREEN · Seen Mar 2026
- Country with the Most EventsPHONE SCREEN · Seen Mar 2026
Similar questions at other companies
- Package Install OrderVerily · ONSITE INTERVIEW
- Validate and Resolve Evaluation ChainsPlaid · PHONE SCREEN · ONSITE INTERVIEWPlaid interview questions
- Page Referrer ReachabilityAmazon · ONSITE INTERVIEW
- Root of the Largest TreeGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
- Query Whether an Island Size ExistsMeta · ONSITE INTERVIEWMeta interview questions
- Maximum Programmer-Problem MatchingGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Reachable Nodes in a Directed GraphSoFi · ONSITE INTERVIEWSoFi interview questions
- Spreadsheet Formula DependenciesAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Task Build Order and File-Change RerunsVanta · PHONE SCREEN
- Resolve Variable Equations with Dependency ErrorsApplied Intuition · PHONE SCREENApplied Intuition interview questions