TikTok·Software Engineer·Technical Phone Screen
- Given a directed or undirected graph with n nodes and m edges where each node has an integer rating, find the top k distinct nodes reachable from a starting node s. Break ties by smaller node ID, avoid revisiting nodes, and return results in descending rating order. Analyze your time and space complexity and justify your data structure choices.
- How does your solution change if k is close to the total number of reachable nodes?
- What happens to your approach if node ratings can change while the traversal is still in progress?
- How would you redesign the solution if the graph is too large to fit in memory?
“I went with BFS plus a min-heap of size k, which felt right.”