Solve graph and linked list tasks
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates competencies in dependency graph modeling and reachability, shortest-path computation in weighted directed graphs, and space-efficient linked-list traversal, testing understanding of graph theory, pathfinding algorithms, and pointer-based data structure techniques.
Part 1: Find All Recipes That Can Be Produced
Constraints
- 0 <= len(recipes) == len(ingredients) <= 10^5
- Recipes are distinct strings.
- 0 <= total number of ingredient references across all recipes <= 2 * 10^5
- 0 <= len(supplies) <= 10^5
- Ingredient names and recipe names are strings.
- Cycles may exist in the dependency graph.
Examples
Input: (['bread', 'sandwich', 'burger'], [['yeast', 'flour'], ['bread', 'meat'], ['sandwich', 'bread']], ['yeast', 'flour', 'meat'])
Expected Output: ['bread', 'sandwich', 'burger']
Explanation: bread can be made first, then sandwich using bread and meat, then burger using sandwich and bread.
Input: (['A', 'B'], [['B'], ['A']], [])
Expected Output: []
Explanation: A and B form a cycle, so neither can be produced.
Input: (['tea', 'milktea', 'juice'], [[], ['tea', 'milk'], ['fruit']], ['milk'])
Expected Output: ['tea', 'milktea']
Explanation: tea needs no ingredients, so it is immediately producible. Then milktea can be made from tea and milk. juice cannot be made.
Input: (['omelet'], [['egg', 'pan']], ['egg'])
Expected Output: []
Explanation: pan is never available, so omelet cannot be produced.
Input: ([], [], ['salt'])
Expected Output: []
Explanation: There are no recipes to produce.
Hints
- Think of each available name as unlocking the recipes that depend on it.
- Track how many required names are still missing for each recipe. When that count becomes 0, the recipe itself becomes available.
Part 2: Signal Propagation Time in a Directed Weighted Graph
Constraints
- 1 <= n <= 10^5
- 0 <= len(edges) <= 2 * 10^5
- 1 <= u, v <= n for every edge (u, v, w)
- 1 <= w <= 10^9
- All edge weights are positive integers.
- Multiple edges between the same pair of nodes may exist.
Examples
Input: (4, [(2, 1, 1), (2, 3, 1), (3, 4, 1)], 2)
Expected Output: 2
Explanation: From node 2, the shortest times are 1 to node 1, 1 to node 3, and 2 to node 4, so the last node receives the signal at time 2.
Input: (2, [(1, 2, 1)], 2)
Expected Output: -1
Explanation: Starting from node 2, node 1 is unreachable.
Input: (1, [], 1)
Expected Output: 0
Explanation: The only node is the start node, so all nodes have the signal at time 0.
Input: (3, [(1, 2, 10), (1, 2, 3), (2, 3, 4), (1, 3, 20)], 1)
Expected Output: 7
Explanation: The best path to node 2 uses weight 3, and then node 3 is reached in 3 + 4 = 7.
Hints
- Because all weights are positive, use a shortest-path algorithm that works well with a priority queue.
- Once you know the shortest distance to every node from k, the answer is the largest of those distances, unless some distance was never updated.
Part 3: Determine Whether a Singly Linked List Has Odd or Even Length
Constraints
- The list may be empty.
- The list may contain up to 10^5 nodes.
- Use O(1) extra space.
- Do not compute the full length with a counter.
Examples
Input: (None,)
Expected Output: 'even'
Explanation: An empty list has length 0, which is even.
Input: ({'val': 7, 'next': None},)
Expected Output: 'odd'
Explanation: A single-node list has odd length.
Input: ({'val': 1, 'next': {'val': 2, 'next': None}},)
Expected Output: 'even'
Explanation: Two nodes give an even length.
Input: ({'val': 1, 'next': {'val': 2, 'next': {'val': 3, 'next': {'val': 4, 'next': {'val': 5, 'next': None}}}}},)
Expected Output: 'odd'
Explanation: This list has 5 nodes, so its length is odd.
Hints
- Try advancing through the list two nodes at a time instead of one.
- If that pointer falls off the list exactly, the length is even. If it stops on the last node, the length is odd.