Longest Cycle in a Graph
A hard-tier problem at 50% community acceptance, tagged with Depth-First Search, Breadth-First Search, Graph. Reported in interviews at Juspay and 0 others.
Longest Cycle in a Graph is a hard problem that tests whether you can identify cycles in directed graphs and track cycle lengths efficiently. With a 50% acceptance rate, it's not a gimme, and Juspay has asked it. The trap is obvious: DFS visits nodes, but building and tracking cycle lengths without revisiting states or blowing up your complexity is where most candidates lose points. If you hit this on a live assessment and blank on the state-tracking mechanics, StealthCoder surfaces a working solution in seconds, invisible to the proctor.
Companies that ask "Longest Cycle in a Graph"
Longest Cycle in a Graph is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround.
Get StealthCoderThe trick is understanding that you need to track both visited nodes and recursion stack state during DFS. A node belongs to a cycle only if you encounter it again while exploring its own subtree. Once you detect a back edge, the cycle length is current depth minus the depth where you first encountered that node. Most candidates either count all paths (wrong), forget to handle disconnected components, or use a topological sort approach that fails on cyclic graphs. The graph is directed, so undirected cycle logic doesn't apply. You'll need to track entry and exit times or a color-based scheme (white, gray, black) to distinguish between back edges, forward edges, and cross edges. StealthCoder is your hedge if the DFS state machine gets tangled during the assessment.
Pattern tags
You know the problem.
Make sure you actually pass it.
Longest Cycle in a Graph recycles across companies for a reason. It's hard-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Longest Cycle in a Graph interview FAQ
How hard is this really compared to other cycle-detection problems?+
It's harder than basic cycle detection because you must track cycle length, not just existence. The 50% acceptance rate reflects that most people get DFS right but fail on the state management and edge classification needed to compute max length correctly.
Do I need topological sort here?+
No. Topological sort is for DAGs. This graph has cycles, so topological sort alone won't work. You need DFS with proper state tracking (visited, recursion stack, entry/exit times) to find back edges and measure cycle length.
What's the most common mistake candidates make?+
Counting path length instead of cycle length, or failing to reset state between disconnected components. Some also confuse visited with in-stack. You must distinguish: a back edge to a node already in the current recursion stack creates a cycle; a back edge to a visited node outside the stack doesn't.
Why is Depth-First Search the right approach here?+
DFS naturally follows paths and detects back edges (edges to ancestors in the DFS tree), which define cycles. BFS doesn't preserve tree structure the same way, making back edge detection much harder. Graph iteration must be DFS-centric to track recursion depth.
Can I solve this without tracking entry and exit times?+
Yes, but it's messier. A color-based approach (white for unvisited, gray for in-stack, black for done) works and is clearer for most people. Either way, you must distinguish nodes in the current path from nodes visited in other paths.
Want the actual problem statement? View "Longest Cycle in a Graph" on LeetCode →