FastPrepTerminal Nodes for Each Source
Problems/Terminal Nodes for Each Source

Terminal Nodes for Each Source

Indeed logoIndeed● MediumINTERNPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The second coding question in a Karat interview was about source and end nodes in a directed graph with no cycles: for each source, list its end nodes. Example 1 is the report's own example.

The report does not define a source or an end node, and it does not fix the output format or order. Here, a source has no incoming edge and a terminal node has no outgoing edge. Each source gets one row with its name first and then its terminal nodes, each listed once. Rows and names are sorted. Names of 1 to 5 capital letters, no repeated edges and the size limits are practice choices.

The judged task matches the report at about 85%.

You are given a list of directed edges. Each edges[i] = [from, to] is an edge from node from to node to. Nodes are named by strings, and every node appears in at least one edge. The edges have no cycle.

A source is a node with no incoming edge. A terminal node is a node with no outgoing edge.

For each source, find every terminal node it can reach by following one or more edges. Return one row per source: the source's name first, then the names of its terminal nodes.

  • List each terminal node once in a row, even when several paths reach it.
  • Sort the rows by source name, and sort the terminal names inside each row. Use plain dictionary order, so "AB" comes before "B".

Function

sourceTerminals(edges: String[][]) → String[][]

Examples

Example 1

edges = [["A","B"],["B","C"],["A","D"],["E","F"]]return = [["A","C","D"],["E","F"]]

A is the only start of the first group: A goes to B and then C, and A also goes straight to D. C and D have no outgoing edge, so they are the terminal nodes for A. E reaches only F.

Example 2

edges = [["P","Q"],["P","R"],["Q","S"],["R","S"],["T","R"],["R","U"]]return = [["P","S","U"],["T","S","U"]]

P and T have no incoming edge, so they are the sources. P reaches S through Q and again through R, but S is listed once. Both sources also reach U through R. Q and R have outgoing edges, so they are not terminal nodes.

Example 3

edges = [["HOME","WORK"],["GYM","HOME"],["GYM","PARK"],["CAFE","PARK"]]return = [["CAFE","PARK"],["GYM","PARK","WORK"]]

The sources are CAFE and GYM; HOME has an incoming edge from GYM. GYM reaches WORK through HOME and reaches PARK directly. CAFE reaches PARK. Rows are sorted by source name, so CAFE comes before GYM.

Constraints

  • 1 <= edges.length <= 1000
  • edges[i].length == 2
  • Each name has 1 to 5 capital letters from A to Z.
  • edges[i][0] != edges[i][1], and no edge appears twice.
  • The edges have no cycle.

More Indeed problems

  • Insert into an Unrolled Linked ListPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Course Completion OrderPHONE SCREEN · Seen May 2025
  • Recipes You Can PreparePHONE SCREEN · Seen May 2025
All Indeed interview questionsSee Indeed hiring insights
Saved
class Solution {
    public String[][] sourceTerminals(String[][] edges) {
        // Write your code here.
    }
}
Checking account…