Solve shortest and label-similar paths
Company: dYdX
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Solve shortest and label-similar paths states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Unweighted Shortest Path with Reconstruction
Constraints
- 1 <= n <= 10^4
- 0 <= number of edges <= 10^4
- edges[i] = [u, v] with 0 <= u, v < n, u != v
- 0 <= s, t < n
- The graph is undirected and unweighted; it may be disconnected and may contain cycles.
Examples
Input: (6, [[0,1],[1,2],[2,3],[1,4],[4,5],[5,3]], 0, 3)
Expected Output: [3, [0, 1, 2, 3]]
Explanation: Shortest route from 0 to 3 is 0->1->2->3 of length 3; the alternative 0->1->4->5->3 has length 4.
Input: (4, [[0,1],[2,3]], 0, 3)
Expected Output: [-1, []]
Explanation: Vertices 0 and 3 lie in different connected components, so no path exists.
Input: (1, [], 0, 0)
Expected Output: [0, [0]]
Explanation: Start equals target: the path is just [0] with length 0.
Input: (5, [[0,1],[1,2],[2,3],[3,4]], 0, 4)
Expected Output: [4, [0, 1, 2, 3, 4]]
Explanation: A simple path graph; the only route from 0 to 4 has length 4.
Input: (3, [[0,1],[1,2],[0,2]], 0, 2)
Expected Output: [1, [0, 2]]
Explanation: The direct edge 0-2 gives a length-1 path, shorter than 0->1->2.
Hints
- BFS explores nodes in order of increasing distance, so the first time you dequeue (or reach) t you already have a shortest path — no need to keep searching.
- Store a parent[] pointer when you first discover each node; to rebuild the path, walk parent[] from t back to s and reverse the result.
- Handle s == t (length 0, path [s]) and the disconnected case (return [-1, []]) explicitly.
Path Matching a Label Sequence with Minimal Mismatches
Constraints
- 1 <= n <= 10^4
- 0 <= number of edges <= 10^4
- 1 <= m <= 10^3 (length of target)
- name has length n; target has length m; labels are arbitrary strings
- Consecutive vertices in the returned walk must be adjacent; vertices may repeat.
- If m >= 2 and the graph has no edges, no valid walk of length m exists; return an empty list.
Examples
Input: (5, [[0,1],[1,2],[2,3],[3,4]], ['a','b','c','d','e'], ['a','b','c'])
Expected Output: [0, 1, 2]
Explanation: Vertices 0,1,2 have labels a,b,c matching target exactly along the path edges 0-1 and 1-2, giving 0 mismatches.
Input: (3, [[0,1],[1,2]], ['x','y','z'], ['a','b','c'])
Expected Output: [0, 1, 0]
Explanation: No label matches any target, so every valid walk costs 3 mismatches; [0,1,0] is one optimal walk (edges 0-1 and 1-0).
Input: (4, [[0,1],[1,2],[2,3],[3,0]], ['p','q','p','q'], ['p','q','p'])
Expected Output: [0, 1, 0]
Explanation: name[0]=p, name[1]=q match target p,q; returning to 0 (label p) matches target[2]=p, so 0 mismatches. (Path 0-1-2 is equally optimal; the deterministic tie-break returns this one.)
Input: (1, [], ['a'], ['a'])
Expected Output: [0]
Explanation: Single vertex, single target label that matches: the walk is just [0] with 0 mismatches.
Input: (2, [[0,1]], ['a','b'], ['b','a','b'])
Expected Output: [1, 0, 1]
Explanation: Vertex 1=b, 0=a, 1=b matches target b,a,b exactly while alternating across the single edge, for 0 mismatches.
Hints
- Think position-by-position: dp[i][v] = cheapest way to align target[0..i] onto a walk ending at vertex v. Initialize dp[0][v] with just the mismatch at v.
- The transition only looks at neighbors of v in the previous position: dp[i][v] = min(dp[i-1][u] for u adjacent to v) + (name[v] != target[i]).
- Keep a parent[i][v] = the u that achieved the minimum, then walk it back from the best end vertex to reconstruct one optimal sequence. Sort adjacency lists for a deterministic tie-break.