Iterative Depth-First Search
Updated
Problem statement
You are given a directed graph as an adjacency list adjacency and a starting vertex start. Vertices are numbered from 0 through adjacency.length - 1. Each neighbor list is ordered.
Implement depth-first search iteratively, without recursive calls. Visit neighbors in their listed order, and append a vertex to the result when it is first visited. Return that preorder of the vertices reachable from start. Visit each vertex only once, even when the graph has cycles, self-loops, or repeated edges. Do not include unreachable vertices.
Function
iterativeDfs(adjacency: int[][], start: int) → int[]Examples
Example 1
adjacency = [[1,2],[2,3],[3],[]]start = 0return = [0,1,2,3]Visit 0, then follow its first neighbor 1. From 1, visit 2, then 3. Later edges to already visited vertices add nothing.
Example 2
adjacency = [[2,1],[3],[3],[1],[]]start = 0return = [0,2,3,1]The first listed neighbor of 0 is 2. Continue through 3 and 1; the edge from 1 back to 3 is skipped. Vertex 4 is unreachable.
Example 3
adjacency = [[],[0],[1]]start = 2return = [2,1,0]From start vertex 2, the only path is 2 → 1 → 0.
Constraints
1 <= adjacency.length <= 100000.0 <= start < adjacency.length.- Every neighbor is a valid vertex number. The graph may contain cycles, self-loops, and repeated edges.
- The sum of all neighbor-list lengths is at most
200000.
More Meta Onsite Interview questions
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Linear Layer Forward and Backward PassesONSITE INTERVIEW · Seen Sep 2026
- Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
- Stochastic Gradient Descent with MomentumONSITE INTERVIEW · Seen Sep 2026
- Top K Largest Values with a HeapONSITE INTERVIEW · Seen Sep 2026
- Partition a Card Deck into FifteensONSITE INTERVIEW · Seen Sep 2026
- Insert into a Sorted Circular ListONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Validate a DFS Traversal OrderRippling · OARippling interview questions
- Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Course ScheduleOracle · ONSITE INTERVIEWOracle interview questions
- Package Install OrderVerily · ONSITE INTERVIEW
- Course SchedulePlaud · ONSITE INTERVIEW
- Course Schedule Cycle DetectionGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions
- Validate and Resolve Evaluation ChainsPlaid · PHONE SCREEN · ONSITE INTERVIEWPlaid interview questions
- Delete and Compact a Forest SubtreePinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Audit Discounted Category AncestorsFaire · ONSITE INTERVIEW
Meta interview experiences
- Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
- Meta Business Support Engineer Phone Screen: 1 Experience
- Meta Software Engineer Onsite Questions: 3 Experiences
- Meta AI Native Engineer OA and Onsite: 1 Experience
- Meta AI-Native Coding Interview: 3 Experiences
- Meta Production Engineer Intern Questions: 6 Experiences