Frontend School

API Endpoint Reachability Check

DSA
easy

API Endpoint Reachability Check

Easy
Graph
BFS
DFS

Overview

In Postman, complex workflows often involve chains of API requests where one request's success or output is a prerequisite for another. Consider a simplified model where we represent these dependencies as a directed graph. Your task is to determine if a specific API endpoint can be reached (i.e., executed) starting from another given endpoint, following the defined dependencies. This is crucial for understanding workflow feasibility and potential execution paths within a collection.

What you need to do / Task

Implement a function canReachEndpoint that takes the total number of endpoints, a list of dependencies, a starting endpoint, and a target endpoint. The function should return true if the targetEndpoint is reachable from the startEndpoint by traversing the dependencies, and false otherwise.

Input & output

  • numEndpoints: An integer representing the total number of API endpoints, indexed from 0 to numEndpoints - 1.
  • dependencies: An array of [u, v] pairs, where each pair indicates a directed dependency from endpoint u to endpoint v. This means u can trigger or depends on v.
  • startEndpoint: An integer representing the initial endpoint from which to begin traversal.
  • targetEndpoint: An integer representing the endpoint we are trying to reach.

Returns:

A boolean value: true if targetEndpoint is reachable from startEndpoint, false otherwise.

Clarifications

  • An endpoint is considered reachable from itself.
  • The graph might contain cycles.
  • Dependencies are directed. [u, v] means u can lead to v, not v to u.

Examples

1

Example 1

Input:

numEndpoints = 4, dependencies = [[0,1],[1,2],[2,3]], startEndpoint = 0, targetEndpoint = 3

Output:

true

Note:

Path: 0 -> 1 -> 2 -> 3. Endpoint 3 is reachable from endpoint 0.

2

Example 2

Input:

numEndpoints = 4, dependencies = [[0,1],[1,2]], startEndpoint = 0, targetEndpoint = 3

Output:

false

Note:

Endpoint 3 is not reachable from endpoint 0. There is no path.

3

Example 3

Input:

numEndpoints = 3, dependencies = [[0,1],[1,0]], startEndpoint = 0, targetEndpoint = 0

Output:

true

Note:

An endpoint is always considered reachable from itself. Also, 0 -> 1 -> 0 is a path.

Constraints

Constraints & assumptions

  • 1 <= numEndpoints <= 1000
  • 0 <= dependencies.length <= numEndpoints * (numEndpoints - 1)
  • 0 <= u, v < numEndpoints for all [u, v] in dependencies.
  • 0 <= startEndpoint, targetEndpoint < numEndpoints
  • The graph may be disconnected.

Out of scope

  • Finding the shortest path.
  • Handling invalid endpoint indices (assume inputs are within bounds).

What interviewers evaluate

  • Ability to correctly model the problem as a graph traversal.
  • Correct implementation of BFS or DFS.
  • Effective handling of visited nodes to prevent infinite loops and redundant work.
  • Correctly addresses base cases and general graph traversal logic.

Loading editor...