The Restore the Array From Adjacent Pairs interview question gives you a 2D array of adjacent pairs from an original array (where each adjacent pair [a, b] means a and b were next to each other), and asks you to reconstruct the original array. The original array has no duplicates, and adjacent pairs are given in any order. Any valid reconstruction is acceptable.
This problem is asked at Visa, Meta, Roblox, SIG, Robinhood, Capital One, and Google because it tests graph construction and traversal. Recognizing that adjacent pairs define a path graph (each node has degree 1 or 2) allows a DFS/BFS traversal from a leaf (degree-1 node) to reconstruct the array. It evaluates a candidate's ability to model non-obvious problem structures as graphs.
The pattern is graph construction followed by DFS path traversal. Build an adjacency list from the pairs. Identify a starting node: in the path graph, the two endpoints have degree 1 (they appear in only one pair). Start DFS from either endpoint. At each node, visit its unvisited neighbor and append it to the result. Continue until all nodes are visited. The traversal order gives the original array.
Adjacent pairs: [[2,1],[3,4],[3,2]]
Adjacency list:
Degree-1 nodes (endpoints): 1 (degree 1) and 4 (degree 1).
Start DFS from 1: 1 → 2 (unvisited) → 3 (unvisited) → 4 (unvisited).
Result: [1, 2, 3, 4].
For the Restore the Array From Adjacent Pairs coding problem, the array, hash table, and DFS interview pattern is key. The insight — "treat adjacent pairs as edges in an undirected graph, find the endpoints, and traverse" — is the core problem reduction. Interviewers at Capital One and Robinhood appreciate when you state this graph modeling insight upfront before writing code. Practice identifying degree-1 nodes as the start of path traversal.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Escape a Large Maze | Hard | Solve | |
| 4Sum II | Medium | Solve | |
| Brick Wall | Medium | Solve | |
| Card Flipping Game | Medium | Solve | |
| Array Nesting | Medium | Solve |