Find Business Degrees of Separation
Company: Intuit
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of graph traversal and shortest-path computation, including modeling business relationships as undirected graphs and interpreting degrees of separation on the shortest path.
Constraints
- 0 <= len(relationships) <= 200000
- Each relationship contains exactly two business identifiers
- Business identifiers can be strings or integers and are compared by equality
- Duplicate relationships may appear and should not affect correctness
Examples
Input: ([('A', 'B'), ('B', 'C')], 'A', 'C')
Expected Output: (['A', 'B', 'C'], 1)
Explanation: The shortest path from A to C is A -> B -> C. The only intermediate business is B, so the degree is 1.
Input: ([('Shop1', 'Shop2'), ('Shop2', 'Shop3'), ('Shop3', 'Shop4')], 'Shop2', 'Shop3')
Expected Output: (['Shop2', 'Shop3'], 0)
Explanation: Shop2 and Shop3 are directly connected, so the shortest path has no intermediate businesses.
Input: ([('A', 'B'), ('B', 'C')], 'B', 'B')
Expected Output: (['B'], 0)
Explanation: When source and target are the same, the shortest path is just that single business, with degree 0.
Input: ([('A', 'B'), ('C', 'D')], 'A', 'D')
Expected Output: ([], -1)
Explanation: A and D are in different disconnected components, so no path exists.
Input: ([('A', 'B'), ('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'E')], 'A', 'E')
Expected Output: (['A', 'B', 'C', 'D', 'E'], 3)
Explanation: Duplicate edges do not change the answer. The shortest path is A -> B -> C -> D -> E, which has three intermediate businesses: B, C, and D.
Input: ([], 'X', 'Y')
Expected Output: ([], -1)
Explanation: With no relationships and different source/target businesses, there is no path.
Hints
- Model the businesses as an unweighted graph. What graph traversal finds the shortest path in an unweighted graph?
- Store the parent of each visited business during traversal so you can reconstruct the path once you reach the target.