← Snapchat Interview Insights

Snapchat·Machine Learning Engineer·Technical Phone Screen·Senior

SeniorRejected
May 2026

Summary

Snapchat ML Engineer interview with a coding round that did not go well. The question was a classic graph problem and I fumbled the implementation badly enough that there's not much else to say.

Questions Asked (1)

Q1

Given a number of courses and a list of prerequisites, determine whether it's possible to complete all courses without getting stuck in a dependency cycle.

Algorithms & Data Structures
Author's notes

Went straight for DFS and fell apart on the visited state logic.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the courses and prerequisites as a directed graph where edges represent dependencies. Then, detect whether the graph contains a cycle using either Kahn's algorithm (BFS-based topological sort) or DFS with recursion stack. If a cycle exists, it's impossible to complete all courses; otherwise, it's possible.

Pro tip: Discuss the trade-offs between Kahn's algorithm and DFS: Kahn's is iterative and avoids recursion depth issues, while DFS can be simpler to implement but may hit stack limits for large graphs. Also, mention that this problem is equivalent to checking if the course dependency graph is a DAG (Directed Acyclic Graph).

1. Clarify the problem

Confirm that the input is a number of courses (n) and a list of prerequisite pairs [a, b] meaning b must be taken before a. Ensure understanding that we need to return true if all courses can be finished, false otherwise.

2. Build the graph

Construct an adjacency list where each course points to its dependents (or prerequisites). Also compute the in-degree for each node if using Kahn's algorithm.

3. Detect cycles

Use either Kahn's algorithm (repeatedly remove nodes with in-degree 0 and count processed nodes) or DFS with a recursion stack to detect back edges. If all nodes are processed (Kahn's) or no back edge is found (DFS), there is no cycle.

4. Return result

If a cycle is detected, return false (impossible to complete all courses). Otherwise, return true.

5. Analyze complexity

State that both approaches run in O(V + E) time and O(V + E) space, where V is the number of courses and E is the number of prerequisites.

Key Points to Mention

  • Graph representation: adjacency list for efficient traversal.
  • Cycle detection algorithms: Kahn's algorithm (BFS) and DFS with recursion stack.
  • Topological sorting: a valid topological order exists iff the graph is a DAG.
  • Time and space complexity: O(V + E) for both algorithms.
  • Handling edge cases: no prerequisites, disconnected components, self-loops.
  • Real-world relevance: course scheduling, task dependency resolution, and ML pipeline orchestration.

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