Reported August 2023
Arcesiumgraph

Detect a Cycle in Directed Hate Relationships

Reported by candidates from Arcesium's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Arcesium OA. Under 2s to a working solution.
Founder's read

The edge case that breaks a naive solution on this Arcesium OA, reported in August 2023, is a graph that isn't connected. Run DFS from node 0 only and you'll miss a cycle hiding in another component. The task is plain: given n politicians and directed hate pairs, return true if the graph has a directed cycle. It's cycle detection in a directed graph, a classic graph problem. Example 2 looks scary because two paths converge on node 3, but that's a diamond, not a cycle. If you blank on the visited-state logic during the live assessment, StealthCoder is the safety net running invisibly on your screen.

The problem

There are n politicians labeled from 0 to n - 1. You are given a list of directed hate relationships hatePairs, where each pair [u, v] means that politician u hates politician v.
Return true if the directed graph formed by these relationships contains a directed cycle. Otherwise, return false.
A directed cycle exists when it is possible to start at a politician, follow one or more hate relationships in their stated direction, and return to the starting politician.

Function
hasDirectedHateCycle(n: int, hatePairs: int[][]) → boolean

Examples
Example 1
n = 4
hatePairs = [[0,1],[1,2],[2,0],[2,3]]
return = true
The relationships 0 -> 1, 1 -> 2, and 2 -> 0 form a directed cycle, so the result is true.
Example 2
n = 4
hatePairs = [[0,1],[0,2],[1,3],[2,3]]
return = false
Every relationship moves toward politician 3, and no path returns to an earlier politician. The graph has no directed cycle, so the result is false.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Two clean approaches work. First, DFS with three states per node: unvisited, in the current recursion path, and done. If you reach a node that's in the current path, that's a back edge, so return true. Second, Kahn's algorithm: compute indegrees, peel off zero-indegree nodes with a queue, and if you process fewer than n nodes, a cycle exists. The pitfall is using a single visited set. That flags the diamond in Example 2 as a cycle, because node 3 gets reached twice. Also loop over all n nodes as start points, since the graph can be disconnected, and watch for self-loops like [u,u], which count as a cycle. Both approaches run in O(n + E). If you freeze live, StealthCoder can hand you the three-state version so you can adapt it quickly.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Detect a Cycle in Directed Hate Relationships 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. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as course schedule. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Arcesium's OA.

Arcesium reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Detect a Cycle in Directed Hate Relationships FAQ

What's the trick to the Arcesium directed hate cycle problem?+

Track node state, not just visited. Use three states: unvisited, in progress, finished. A cycle exists only when DFS hits a node that's still in progress. Alternatively use Kahn's topological sort and check whether every node got processed.

Why does a plain visited set give the wrong answer?+

Because reaching an already-visited node doesn't mean a cycle in a directed graph. In Example 2, nodes 1 and 2 both lead to node 3. A single visited set would flag node 3 on the second arrival, returning true when the right answer is false.

Do I need to start DFS from every node?+

Yes. The graph can have several disconnected pieces, and a cycle might live in one you never reach from node 0. Loop through all n nodes and start a DFS from each unvisited one. Isolated nodes with no edges are fine and just return quickly.

How hard is this one really?+

It's a standard medium, equivalent to checking whether a course schedule is feasible. If you know DFS coloring or Kahn's algorithm, it's about 20 lines. The difficulty is in the edge cases: disconnected components, self-loops, and diamond shapes that look like cycles.

How do I prepare in 48 hours?+

Write the DFS three-state version and the Kahn's version from memory, once each. Test them on both examples plus a self-loop and a disconnected graph. Build the adjacency list first. That covers nearly everything this problem can throw at you.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Arcesium.

OA at Arcesium?
Invisible during screen share
Get it