All practice tasks

Coding (Algo)

Count Reachable Services

Graph traversal · Easy

A dependency map records which service can call which other service. Starting from one service, an impact check counts all services reachable through directed calls, including the start itself. Services in other disconnected regions must not be counted.

Count the distinct nodes reachable from source by following zero or more directed edges and ignoring repeated visits.

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 an integer count, including source even when it has no outgoing edges.
  • A missing source returns zero. A cycle or duplicate edge must not increase the count.

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,
      1
    ],
    [
      1,
      0
    ],
    [
      2,
      3
    ]
  ],
  "n": 4,
  "source": 0
}

Returns

2

Example 2

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

Returns

1

Required complexity

O(n + m) time and O(n + m) space including adjacency

What this task practices

Count Reachable Services 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 easy 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.