Implement topological sort from string input
Company: Netflix
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: This question evaluates a candidate's skills in parsing unstructured dependency strings into directed graphs, producing a valid topological order, detecting and reporting minimal cycles, and reasoning about robustness against duplicate, unknown, or malformed inputs as well as time and space complexity.
Part 1: Parse Dependency Strings into a Directed Graph
Constraints
- 0 <= len(lines) <= 10^4
- Total input length across all lines <= 2 * 10^5 characters
- Task names contain only letters, digits, and underscores
Examples
Input: ['install A after B', 'C->A', 'B before D']
Expected Output: {'A': [], 'B': ['A', 'D'], 'C': ['A'], 'D': []}
Explanation: Three different input formats all contribute edges to the same graph.
Input: [' Task1 -> Task2 ']
Expected Output: {'Task1': ['Task2'], 'Task2': []}
Explanation: Spaces around the arrow should be ignored.
Input: []
Expected Output: {}
Explanation: Edge case: no lines means no tasks and no edges.
Input: ['A before A']
Expected Output: {'A': ['A']}
Explanation: A self-dependency is still a valid directed edge at parse time.
Solution
def solution(lines):
def valid(task):
return bool(task) and all(ch.isalnum() or ch == '_' for ch in task)
graph = {}
def ensure(node):
if node not in graph:
graph[node] = set()
for line in lines:
s = line.strip()
if not s:
continue
edge = None
if '->' in s:
parts = s.split('->')
if len(parts) == 2:
a, b = parts[0].strip(), parts[1].strip()
if valid(a) and valid(b):
edge = (a, b)
else:
tokens = s.split()
if len(tokens) == 3 and tokens[1] == 'before' and valid(tokens[0]) and valid(tokens[2]):
edge = (tokens[0], tokens[2])
elif len(tokens) == 4 and tokens[0] == 'install' and tokens[2] == 'after' and valid(tokens[1]) and valid(tokens[3]):
edge = (tokens[3], tokens[1])
if edge is None:
continue
u, v = edge
ensure(u)
ensure(v)
graph[u].add(v)
return {node: sorted(graph[node]) for node in graph}