Dsa
Graphs Dfs
Easy
Trade Route Checker
You are given a directed graph representing a set of trade routes between cities. Each route connects one city to another. Your task is to determine if there is a cycle in the trade routes graph. Implement the function `has_cycle(routes: List[Tuple[str, str]]) -> bool` that returns True if there is a cycle; otherwise return False.
Input format:
- The first line contains an integer `n` for the number of routes.
- The next `n` lines each contain a pair of strings `u v`, indicating a directed route from city `u` to city `v`.
Output format:
- Print 'Yes' if there is a cycle, otherwise print 'No'.
Example:
Input:
4
A B
B C
C D
D A
Output:
Yes
Key concepts
graphsdfscycle detection
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now