Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Problem 3: Minimum Number of Vertices to Reach All Nodes(medium)

Problem Statement

Given a directed acyclic graph with n nodes labeled from 0 to n-1, determine the smallest number of initial nodes such that you can access all the nodes by traversing edges. Return these nodes.


  1. Input:

    • n = 6
    • edges = [[0,1],[0,2],[2,5],[3,4],[4,2]]

    Expected Output: [0,3]

    Justification: Starting from nodes 0 and 3, you can reach all other nodes in the graph. Starting from node 0, you can reach nodes 1, 2, and 5. Starting from node 3, you can reach nodes 4 and 2 (and by extension 5).

  2. Input:




Like the course? Get enrolled and start learning!