Validate a Directed Edge Addition
Updated
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[]) → booleanExamples
Example 1
edges = [[1,2],[2,3],[3,4]]newEdge = [4,1]return = falseAdding 4 -> 1 creates the cycle 1 -> 2 -> 3 -> 4 -> 1.
Example 2
edges = [[1,2],[2,3],[3,4]]newEdge = [1,2]return = falseThe exact edge 1 -> 2 already exists.
Example 3
edges = [[1,2],[2,3]]newEdge = [1,3]return = trueThe edge is new and does not create a directed cycle.
More Hive Phone Screen questions
Similar questions at other companies
- Detonate the Maximum BombsZomato / Eternal · PHONE SCREENZomato / Eternal interview questions
- Task Build Order and File-Change RerunsVanta · PHONE SCREEN
- Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
- All Halfway Courses Across Course TracksCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Course Schedule Cycle DetectionGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Resolve Variable Equations with Dependency ErrorsApplied Intuition · PHONE SCREENApplied Intuition interview questions
- Evaluate DivisionApple · PHONE SCREENApple interview questions
- Nested Substitutions with Cycle DetectionKikoff · PHONE SCREENKikoff interview questions
- Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions
- Find the Unique User with Access to Every CameraVerkada · PHONE SCREENVerkada interview questions