All practice tasks

Coding (Algo)

Visit Reachable Nodes in Queue Order

Breadth first search · Medium

A graph-inspection tool shows the order in which breadth-first exploration reaches nodes from a selected start. The displayed order must be reproducible even when edges arrive in a different input order, and shared children must appear only once.

Return the breadth-first visitation sequence from source while considering each visited node’s outgoing neighbors in ascending numeric order.

Input

Call

  • Implement solve(input) and return the result. The provided adapter handles JSON input and output.

Input shape

{ n: integer, edges: [integer, integer][], source: integer }

Fields

  • n is the number of nodes labeled 0 through n-1. edges is an array of directed [from,to] pairs.
  • Parallel edges and self-loops are allowed. For an undirected connection supply both directions.
  • source is the starting node. source = -1 means no starting node and yields no reachable nodes.

Output

Return value

  • Return each reachable node once, starting with source. Mark discovery when enqueuing, not after processing.
  • Sort each node’s outgoing neighbors; do not sort an entire BFS level after discovery. No source gives [].

Constraints

  • 0 <= n <= 100000; 0 <= edges.length <= 200000; every endpoint is a valid node label.
  • source and target, when used, are either -1 or valid node labels. Empty graphs use -1.

Examples

Example 1

{
  "edges": [
    [
      0,
      2
    ],
    [
      0,
      1
    ],
    [
      1,
      4
    ],
    [
      2,
      3
    ]
  ],
  "n": 5,
  "source": 0
}

Returns

[
  0,
  1,
  2,
  4,
  3
]

Example 2

{
  "edges": [
    [
      0,
      1
    ],
    [
      0,
      1
    ]
  ],
  "n": 2,
  "source": 0
}

Returns

[
  0,
  1
]

Required complexity

O(n + m + sum(d × log d)) time and O(n + m) space including adjacency, with outgoing degree d

What this task practices

Visit Reachable Nodes in Queue Order is a coding (algo) interview exercise that trains prompt interpretation, explicit assumptions, a concrete response, and a clear explanation of tradeoffs. The catalog marks it as medium difficulty. It focuses on Technical Reasoning, Testing, Validation. The signed-in workspace provides the tools for the round and evaluates the attempt against task-specific criteria. Reference solutions, hidden checks, evaluator instructions, and candidate work remain private.

Sign in to startThe workspace and evaluation open after sign in.