Bird
Raised Fist0

What is the output of the allPathsSourceTarget function when the input graph is [[ ]], i.e., a single node with no edges?

medium🧾 Code Trace Q4 of Q15
Topological Sort - All Paths From Source to Target (DAG)
What is the output of the allPathsSourceTarget function when the input graph is [[ ]], i.e., a single node with no edges?
AError due to empty adjacency list
B[[0,0]]
C[]
D[[0]]
Step-by-Step Solution
Solution:
  1. Step 1: Identify target and initial stack

    Graph has one node (0), target = 0, stack = [(0,[0])].
  2. Step 2: Pop stack and check if node == target

    Pop (0,[0]), node == target, append path [0] to results.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Single node graph returns path with just that node [OK]
Quick Trick: Single node graph -> path is [0] [OK]
Common Mistakes:
MISTAKES
  • Returning empty list instead of path
  • Assuming error on empty adjacency
Trap Explanation:
PITFALL
  • Candidates often forget that source == target means path is just [0], not empty or error.
Interviewer Note:
CONTEXT
  • Tests handling of edge cases with minimal graph size.
Master "All Paths From Source to Target (DAG)" in Topological Sort

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Topological Sort Quizzes