Challenge: Calculate the Number of Nodes in a Graph Level

Given a graph, calculate the number of nodes in a graph at a given level.

Problem statement

Implement a function that returns the number of nodes at a given level starting from a root node of a directed graph.

Try modifying the breadth-first traversal algorithm to achieve this goal.

To solve this problem, all the previously-implemented data structures will be available to us.


An undirected graph represented as an adjacency list, and the level whose number of nodes we need to find


The number of nodes returned as a simple integer

Sample input


Vertex Edges
0 2, 1
1 4, 3
2 None
3 None
4 None

Level: 1

Sample output


Level up your interview prep. Join Educative to access 70+ hands-on prep courses.