Recursive SQL for a BFS: The ultimate interview flex
Description
This is the first image in a series, as indicated by a large orange arrow with the word 'NEXT'. The image is a screenshot of a tweet from user Deedy (@deedydas) about a surprising coding interview experience at Facebook. The tweet reads: 'The most bizarre coding interview I've ever done was at Facebook when as usual I asked a candidate to write in any language of their choice.. And they nonchalantly said "I'll write it in SQL", to which I almost let loose a chuckle until...'. Below this text is a dark-themed code editor window displaying the beginning of a SQL query. The code snippet shows a recursive Common Table Expression (CTE) to implement a Breadth-First Search (BFS) algorithm, starting with 'WITH RECURSIVE bfs AS'. The technical context is the impressive and unconventional use of SQL to solve a complex graph traversal problem typically handled by general-purpose programming languages. This resonates with senior engineers who appreciate deep, non-obvious knowledge of tools, showcasing SQL's Turing-completeness in a practical, high-stakes interview setting
Comments
7Comment deleted
Most candidates try to solve the problem. This one solved the interview. The only thing more surprising would be if they calculated Big O notation using only window functions
He wrote BFS as a recursive CTE; anyone who’s willing to let the query planner manage their queue is exactly the sort of optimistic masochist I want untangling our microservice graph
The same developer who implements BFS in SQL probably has a Kubernetes cluster running their personal blog and uses event sourcing for their grocery list
When the candidate said they'd implement BFS in SQL, the interviewer thought they were about to witness a career-ending mistake. Instead, they got a masterclass in why 'any language' should come with an asterisk - because somewhere out there, a senior engineer is absolutely convinced that recursive CTEs are the hill they'll die on, and honestly, after seeing this implementation with VARCHAR(1000) path tracking, they might just be right. It's the database equivalent of showing up to a knife fight with a spoon and somehow winning
Implementing BFS in SQL - because nothing says interview flex like a queue with ACID semantics and an execution plan as your hiring bar
BFS in recursive SQL CTE: Brilliant interview hack, until your graph hits recursion depth and turns into a DFS abyss
Nothing humbles an interviewer faster than watching BFS emerge from a WITH RECURSIVE; you came for Big‑O, you left debating cycle detection, index hints, and whether the optimizer is your queue