PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

DSA · Theory

Detect cycle in a directed graph

← All stacks

Theory

155/810

Detect cycle in a directed graph

If you can teach Detect cycle in a directed graph using bus routes, you know it. If you only know the heading, you don’t.

Detect cycle in a directed graph on the board: BFS queue / DFS stack. Then say what bus routes looks like after.

Without Detect cycle in a directed graph, a city map gets messy and bus routes is hard to trust.

Detect cycle in a directed graph shows up in a city map. Name bus routes, not “a real-world scenario”.

Don’t do this with Detect cycle in a directed graph: no visited set → infinite loop. Interviewers spot it in ten seconds.

After Detect cycle in a directed graph, Priya should still remember no visited set → infinite loop.

Viva for Detect cycle in a directed graph: what it is → BFS queue / DFS stack → the mistake (no visited set → infinite loop).

Exam tip

For Detect cycle in a directed graph: definition + a city map + one failure.

Example

# adjacency list
graph = {
    "A": [("B", 2), ("C", 5)],
    "B": [("A", 2), ("C", 1)],
    "C": [("A", 5), ("B", 1)],
}
for u, edges in graph.items():
    print(u, "->", edges)

Detect cycle in a directed graph — graphs are often stored as adjacency lists: vertex → list of (neighbor, weight).

Short notes

  • DefDetect cycle in a directed graph — Priya uses it for bus routes in a city map.
  • RuleDetect cycle in a directed graph → BFS queue / DFS stack.
  • RememberDetect cycle in a directed graph + a dry-run table (a city map).
  • UseDetect cycle in a directed graph in a city map (bus routes).
  • TrapDetect cycle in a directed graph — no visited set → infinite loop.
  • ExDetect cycle in a directed graph → bus routes.

Questions

1

Priya asks: why does Detect cycle in a directed graph exist? Use bus routes.

2

When would Priya actually reach for Detect cycle in a directed graph?

3

Viva: one wrong answer people give for Detect cycle in a directed graph.

4

Write the smallest Detect cycle in a directed graph step on bus routes. What happens?

Previous← Reverse a stack using recursionNextOptimal Binary Search Tree →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.