Find Shortest Wiki Click Path
Company: Snowflake
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of graph traversal and shortest-path concepts, testing competencies in algorithm design, handling cycles, complexity analysis, and integration with external link-retrieval APIs.
Constraints
- 0 <= number of pages in wiki_graph <= 10^5
- 0 <= total number of directed links <= 2 * 10^5
- The graph is directed and may contain cycles
- Pages may appear in link lists even if they are not keys in wiki_graph
Examples
Input: ({'/A': ['/B', '/C'], '/B': ['/D'], '/C': ['/D', '/E'], '/D': ['/F'], '/E': ['/F'], '/F': []}, '/A', '/F')
Expected Output: 3
Explanation: The shortest paths are /A -> /B -> /D -> /F and /A -> /C -> /E -> /F, both requiring 3 clicks.
Input: ({'/A': ['/B']}, '/A', '/A')
Expected Output: 0
Explanation: The start and target pages are the same, so no clicks are needed.
Input: ({'/A': ['/B'], '/B': [], '/C': ['/D'], '/D': []}, '/A', '/D')
Expected Output: -1
Explanation: There is no directed path from /A to /D, so the target is unreachable.
Input: ({'/A': ['/B'], '/B': ['/C', '/D'], '/C': ['/A'], '/D': ['/E'], '/E': []}, '/A', '/E')
Expected Output: 3
Explanation: Although there is a cycle /A -> /B -> /C -> /A, the shortest route to /E is /A -> /B -> /D -> /E.
Input: ({'/A': ['/B', '/B'], '/B': ['/C']}, '/A', '/C')
Expected Output: 2
Explanation: Duplicate links should not affect the answer. The shortest path is /A -> /B -> /C, which takes 2 clicks. /C is not a key, so it is treated as having no outgoing links.
Hints
- This is a shortest-path problem on an unweighted graph, so think about breadth-first search.
- Mark pages as visited when you add them to the queue to avoid revisiting nodes in cycles.