Validate a Directed Edge Addition
Reported by candidates from Hive's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Hive OA, reported in July 2026, is running a full cycle detection on the whole graph when you only need one reachability check. The task is a directed graph with one proposed edge. Return false if the edge already exists or if adding it makes a cycle. It looks like a topological sort problem, but it's a lot smaller. If you've got an invite in your inbox, read the next part carefully. StealthCoder sits invisibly on your screen as a safety net if you blank during the live assessment, but the idea here is simple enough to hold in your head.
The problem
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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: adding edge u -> v creates a cycle only if v can already reach u. So build an adjacency list, then run DFS or BFS from v and check whether you hit u. That's one traversal, O(V + E). Check the duplicate case first with a set of pairs, or just scan the edge list. The common pitfall is running full cycle detection with colors or Kahn's algorithm after inserting the edge. It works but it's more code and more bugs. The other pitfall is the self-loop. If u equals v, then v trivially reaches u, so return false. Also watch for nodes that appear only in newEdge and not in edges. Your map lookup should default to an empty list. Use an iterative traversal if you're worried about deep recursion. If you freeze in the live OA, StealthCoder can hand you this reachability approach so you can verify it against the three examples.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Validate a Directed Edge Addition cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Hive's OA.
Hive reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Validate a Directed Edge Addition FAQ
What's the trick to Validate a Directed Edge Addition?+
Adding u -> v makes a cycle only if v already reaches u. Build the adjacency list from the existing edges, then run one DFS or BFS starting at v and look for u. If you find it, return false. Otherwise return true, after the duplicate check.
How hard is this problem really?+
Easy to medium. The code is short, but people overthink it and reach for topological sort. If you spot the reachability idea, it's about 20 lines. The difficulty is recognizing that you don't need to analyze the whole graph.
What edge cases should I test before submitting?+
Test the exact duplicate edge, a self-loop like [3,3], and a new node that isn't in the existing edges. Also test an empty edge list. Missing keys in your adjacency map are the usual crash, so default to an empty list.
Should I use DFS or BFS here?+
Either works. Both are O(V + E). BFS with a queue avoids recursion depth problems on long chains, so it's the safer pick if the graph could be large. Use a visited set either way so you don't loop forever on existing cycles.
How do I prepare for this in 48 hours?+
Practice building an adjacency list from an edge array and writing a reachability check from memory. Then run the three given examples by hand. Graph reachability shows up often in OAs, so that one template covers a lot of ground.