2dbi

Graph problem solvable with BFS

viaGlassdoor

Problem A graph coding challenge (easy-to-medium) that is naturally solved with a breadth-first traversal — for example shortest path in an unweighted graph, reachability, or level-order processing.

Input / Output

  • Input: a graph as an adjacency list / grid plus one or more source vertices.
  • Output: the BFS-derived answer (e.g. shortest distance, reachable set, or levels).

Constraints

  • Graph may be large (up to ~10^5 nodes/edges); the solution should be linear in nodes + edges.

Example

  • From a source node, BFS layer by layer yields the minimum number of edges to every reachable node.

Expected approach

  • Standard BFS with a queue and a visited set/array; process nodes level by level. O(V + E) time and O(V) space. Clearly explaining the modelling and traversal is important.
Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account