Solve shortest and label-similar paths
Problem Description
This problem consists of two independent parts, both involving graph traversal.
Part A: Unweighted Shortest Path
You are given an undirected, unweighted graph with vertices labeled from 0 to n-1 and an edge list.
Given a start node s and a target node t, your task is to return both the length of a shortest path and one actual shortest path (as a list of vertices). If no path exists between s and t, return an empty list.
You should discuss your chosen algorithm and analyze its time and space complexity.
Part B: Path Matching a Label Sequence with Minimal Mismatches
Each vertex i in the graph has an associated string label, name[i].
You are given a sequence target[0..m-1] of labels.
Your task is to find a path v_0, v_1, ..., v_{m-1} such that:
- Consecutive vertices
v_iandv_{i+1}are adjacent in the graph for all0 <= i < m-1. - The number of indices
iwherename[v_i] != target[i]is minimized.
Return one such optimal vertex sequence.
You should explain your approach, describe how you reconstruct the path, and analyze the time and space complexity for the given constraints:
n(number of vertices) up to10^4edges(number of edges) up to10^4m(length of target sequence) up to10^3
AI interview prep
Core dilemma
Balancing efficient graph traversal for shortest paths with the complexities of sequence alignment and state-space optimization in Part B, especially under given constraints.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Solve shortest and label-similar paths
Problem Description
This problem consists of two independent parts, both involving graph traversal.
Part A: Unweighted Shortest Path
You are given an undirected, unweighted graph with vertices labeled from 0 to n-1 and an edge list.
Given a start node s and a target node t, your task is to return both the length of a shortest path and one actual shortest path (as a list of vertices). If no path exists between s and t, return an empty list.
You should discuss your chosen algorithm and analyze its time and space complexity.
Part B: Path Matching a Label Sequence with Minimal Mismatches
Each vertex i in the graph has an associated string label, name[i].
You are given a sequence target[0..m-1] of labels.
Your task is to find a path v_0, v_1, ..., v_{m-1} such that:
- Consecutive vertices
v_iandv_{i+1}are adjacent in the graph for all0 <= i < m-1. - The number of indices
iwherename[v_i] != target[i]is minimized.
Return one such optimal vertex sequence.
You should explain your approach, describe how you reconstruct the path, and analyze the time and space complexity for the given constraints:
n(number of vertices) up to10^4edges(number of edges) up to10^4m(length of target sequence) up to10^3