Reported September 2026
MakeMyTripgraph

Detect a Cycle in a Directed Graph

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

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

With n up to 100000 and 200000 edges, MakeMyTrip's September 2026 OA question rules out anything that restarts a search from scratch for every vertex. That's the whole point. You need one linear pass over the graph, O(n + m), and you need to know which kind of search gets you there. It's directed cycle detection, a graph problem with a short, well-known answer. If you've seen it, it's ten minutes. If your mind goes blank under the timer, StealthCoder runs invisibly on your screen as a safety net and gives you the solution in real time. Either way, learn the trick below first.

The problem

Given n vertices numbered from 0 to n - 1 and a list of directed edges, return whether the graph contains at least one directed cycle.

Function
hasDirectedCycle(n: int, edges: int[][]) → boolean

Examples
Example 1
n = 4
edges = [[0,1],[1,2],[2,0],[2,3]]
return = true
The path 0 -> 1 -> 2 -> 0 is a directed cycle.
Example 2
n = 4
edges = [[0,1],[0,2],[1,3],[2,3]]
return = false
No directed path returns to an active ancestor.

Constraints
1 <= n <= 100000
0 <= edges.length <= 200000
Every edge contains two valid vertex indices.
Parallel edges may appear.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is three-state DFS coloring, or Kahn's algorithm for topological sort. For DFS, mark each node unvisited, in-progress, or done. If you hit an in-progress node while exploring, you've found a back edge, so there's a cycle. If you finish a node, mark it done and never revisit it. That gives O(n + m). The common pitfall is using a single visited set, which flags false cycles in example 2, where node 3 is reached by two paths. Another trap is recursion depth. With n at 100000, a long chain can overflow the stack in some languages, so use an iterative DFS or Kahn's. Kahn's is simpler: compute in-degrees, process zero in-degree nodes from a queue, and if you process fewer than n nodes, a cycle exists. Parallel edges are fine, since in-degree counts them consistently. Don't forget disconnected components. If you blank mid-assessment, StealthCoder is the hedge.

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 a Directed Graph 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 MakeMyTrip's OA.

MakeMyTrip 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 a Directed Graph FAQ

What's the trick to the MakeMyTrip directed cycle question?+

Track three states per node in DFS: unvisited, in-progress, done. Reaching an in-progress node means a back edge, so a cycle exists. A plain visited set gives false positives when two paths converge on the same node, like node 3 in example 2.

Should I use DFS or Kahn's algorithm?+

Either works in O(n + m). Kahn's is easier to write without bugs and avoids recursion depth issues at n = 100000. Count processed nodes, and if the count is less than n, a cycle exists. Pick whichever you can write cleanly from memory.

Will recursion overflow with n up to 100000?+

It can. A single long chain makes recursion 100000 deep, which breaks in many languages by default. Use an iterative DFS with an explicit stack, or use Kahn's algorithm with a queue. That's the safest choice for these constraints.

How do parallel edges and disconnected graphs affect the solution?+

Parallel edges are harmless. In Kahn's, in-degree counts each copy and each gets decremented. In DFS, revisiting a done node is skipped. For disconnected graphs, loop over every vertex from 0 to n - 1 and start a search from each unvisited one.

How do I prepare for this in 48 hours?+

Write both versions once from scratch: three-color DFS and Kahn's. Test them on both examples plus a self-loop and an empty edge list. Build the adjacency list efficiently. If you can do that without looking anything up, you're ready for this pattern.

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

OA at MakeMyTrip?
Invisible during screen share
Get it