Problem · Graph

Validate a Directed Edge Addition

Learn this problem
MediumHive AIFULLTIMEPHONE SCREEN

Problem statement

You are given a directed graph as a list of source-destination pairs and one proposed directed edge.

Return false if the exact proposed edge already exists or if adding it would create a directed cycle. Otherwise, return true.

Function

isValidEdgeAddition(edges: int[][], newEdge: int[]) → boolean

Examples

Example 1

edges = [[1,2],[2,3],[3,4]]newEdge = [4,1]return = false

Adding 4 -> 1 creates the cycle 1 -> 2 -> 3 -> 4 -> 1.

Example 2

edges = [[1,2],[2,3],[3,4]]newEdge = [1,2]return = false

The exact edge 1 -> 2 already exists.

Example 3

edges = [[1,2],[2,3]]newEdge = [1,3]return = true

The edge is new and does not create a directed cycle.

drafts saved locally
public boolean isValidEdgeAddition(int[][] edges, int[] newEdge) {
  // write your code here
}
edges[[1,2],[2,3],[3,4]]
newEdge[4,1]
expectedfalse
checking account