HARDasked at 1 company

Shortest Cycle in a Graph

A hard-tier problem at 37% community acceptance, tagged with Breadth-First Search, Graph. Reported in interviews at Zomato and 0 others.

Founder's read

Shortest Cycle in a Graph is a hard graph problem with a 37% acceptance rate, and Zomato has asked it. You'll see candidates freeze on this one because the obvious BFS or DFS approach doesn't immediately surface the cycle length. The trick isn't obvious until you've either seen it before or have a tool like StealthCoder feeding you the pattern in real time. If you're prepping for Zomato or a general backend role, this is the kind of hard problem that separates people who've drilled graphs from people who haven't. Miss it in the live assessment and you've tanked the round. Nail it and you've bought yourself credibility.

Companies asking
1
Difficulty
HARD
Acceptance
37%

Companies that ask "Shortest Cycle in a Graph"

If this hits your live OA

Shortest 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 by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage.

Get StealthCoder
What this means

The core insight is that BFS can find the shortest cycle if you track parent nodes correctly and detect when you hit a visited node that isn't your immediate parent. Most candidates start with standard cycle detection (DFS with colors or visited sets) but that tells you whether a cycle exists, not its length. BFS from each unvisited node, keeping track of depth and parent, lets you compute the shortest cycle the moment you revisit a node. The trap is off-by-one errors in cycle length calculation and forgetting that an undirected edge connects two nodes, so you need to avoid backtracking to the parent. If you blank on the BFS parent-tracking pattern during the live OA, StealthCoder surfaces a working solution in seconds, invisible to the proctor, so you can paste and move forward.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Shortest 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 by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Shortest Cycle in a Graph interview FAQ

Is this asking for the shortest cycle in a directed or undirected graph?+

The problem doesn't specify in the title, but both variants exist in the wild. Usually it's undirected (simpler to reason about, more common in interview). Make sure you clarify with the interviewer. If undirected, you need parent tracking in BFS to avoid counting the immediate backtrack as a cycle.

What's the trap that makes this hard?+

Standard cycle detection returns true or false. This asks for the length. Candidates build DFS cycle detection, realize it doesn't give length, panic, and restart. BFS with depth tracking and parent awareness is the right pattern. Off-by-one errors on cycle calculation are also common.

Is this still asked at Zomato and backend companies?+

Zomato has confirmed it. It's a legitimate hard interview problem for backend and systems roles. Lower frequency than easy-medium problems, but it pops up enough that it's on your radar if you're targeting them.

How does this relate to Breadth-First Search vs Depth-First Search?+

BFS is the right tool here because it explores by layers and distance is natural. DFS can detect cycles but extracting length is awkward. BFS with parent tracking lets you compute cycle length the moment you hit a visited non-parent node.

What's the time complexity and is it acceptable?+

BFS from each unvisited node is O(V + E) per node, so O(V * (V + E)) worst case. For sparse graphs it's fine. For dense graphs it's slower but still acceptable for interview. Early exit helps if you find a small cycle fast.

Want the actual problem statement? View "Shortest Cycle in a Graph" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.