← Bytedance Interview Insights

Bytedance·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Bytedance SWE interview, just one algorithmic question that turned into a follow-up on cycle detection. Short session but it stuck with me.

Questions Asked (1)

Q1

How do you detect a cycle in a graph using depth-first search?

Algorithms & Data Structures
Author's notes

This was a follow-up, so I was already mid-thought on something else when it landed.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Explain that DFS detects cycles by tracking nodes in the current recursion stack (for directed graphs) or by checking for visited neighbors that are not the parent (for undirected graphs). Then outline the algorithm: perform DFS, mark nodes as visited and in-stack, and if you encounter a node already in the stack, a cycle exists.

Pro tip: Clarify the distinction between directed and undirected graphs early, as the cycle detection logic differs; for directed graphs, use a recursion stack, while for undirected graphs, track the parent to avoid false positives from back-and-forth edges.

1. Clarify graph type

Ask whether the graph is directed or undirected, as the cycle detection approach varies. Mention that for directed graphs, a back edge to a node in the current recursion stack indicates a cycle, while for undirected graphs, a visited neighbor that is not the parent indicates a cycle.

2. Describe DFS traversal with state tracking

Explain that you perform a depth-first search while maintaining two sets: one for all visited nodes and one for nodes currently in the recursion stack (for directed graphs). For undirected graphs, you only need a visited set and track the parent of each node.

3. Detect cycle during traversal

During DFS, when exploring neighbors, if a neighbor is in the recursion stack (directed) or is visited and not the parent (undirected), a cycle is detected. Return true immediately.

4. Handle disconnected graphs

Emphasize that you must run the DFS from every unvisited node to ensure all components are checked, as cycles may exist in disconnected parts of the graph.

5. Analyze complexity and edge cases

State that time complexity is O(V+E) and space complexity is O(V) due to recursion stack and visited sets. Mention edge cases like self-loops and parallel edges.

Key Points to Mention

  • Recursion stack (or color-coding with white/gray/black) for directed graphs
  • Parent tracking to avoid false positives in undirected graphs
  • Handling disconnected components by iterating over all vertices
  • Time and space complexity: O(V+E) time, O(V) space
  • Edge cases: self-loops, parallel edges, and graphs with no edges
  • Alternative approaches like union-find for undirected graphs, but DFS is required here

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.