FastPrepReachable Nodes in a Directed Graph
Problems/Reachable Nodes in a Directed Graph

Reachable Nodes in a Directed Graph

SoFi logoSoFi● MediumFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Find the vertices reachable in a directed graph.

You are given a directed graph. In an object-oriented version, the graph is represented by Vertex objects, and each vertex contains a collection of outgoing neighbors.

Here, each vertex is represented by a unique string id. The input graph is an adjacency list where each row starts with a vertex id, followed by that vertex's outgoing neighbors. A row with only one value represents a vertex with no outgoing neighbors.

Implement the function reachableNodes.

The function should return all vertices that can be reached from start, including start itself.

The graph may contain cycles. Your implementation must avoid visiting the same vertex repeatedly and must not get stuck in an infinite loop.

The returned collection does not need to be in any specific order.

Clarifications / Corner Cases

  • The graph is directed.
  • The result should include the starting vertex.
  • The graph may contain cycles or self-loops.
  • The graph may be disconnected.
  • A vertex may have no outgoing neighbors.
  • The output order does not matter.
  • Assume start is not null.
  • Assume each vertex can be uniquely identified, either by object identity or by a unique id/name.

Follow-up discussion

  1. How would you implement this using DFS?
  2. How would you implement this using BFS?
  3. What state is necessary to prevent repeated work or infinite loops?
  4. Is a parent map necessary for this problem? Why or why not?
  5. What can go wrong with recursive DFS on a very deep graph?
  6. When would BFS be preferable to DFS?
  7. If the graph has thousands of vertices in a long chain, what implementation choice is safer in Java?

Function

reachableNodes(graph: String[][], start: String) → String[]

Examples

Example 1

graph = [["A", "B"], ["B", "C", "D"], ["C"], ["D", "E"], ["E", "B"]]start = "A"return = ["A", "B", "C", "D", "E"]

Given the directed graph:

A -> B
B -> C, D
D -> E
E -> B

Starting from A, we can reach B. From B, we can reach C and D. From D, we can reach E. From E, we go back to B, but B has already been visited.

One valid return value is [A, B, C, D, E].

Example 2

graph = [["A", "B"], ["B"], ["C", "D"], ["D"]]start = "A"return = ["A", "B"]

C and D exist in the graph, but they are not reachable from A.

Example 3

graph = [["A", "A"]]start = "A"return = ["A"]

A has a self-cycle. The algorithm should include A once and stop.

Constraints

  • 1 <= number of vertices <= 100,000
  • 0 <= number of directed edges <= 300,000
  • The graph may be sparse, dense, shallow, or very deep.
  • Only vertices reachable from start need to be returned.

More SoFi problems

  • Job SchedulingOA · Seen Aug 2024
All SoFi interview questionsSee SoFi hiring insights
Saved
class Solution {
    public String[] reachableNodes(String[][] graph, String start) {
      // write your code here
    }

}
Checking account…