When To Choose BFS Over DFS? (Essential For Coding Rounds)

samsung logo
samsung
November 20, 20253 reads

Summary

I participated in an on-campus coding round with Samsung where I encountered a challenging graph problem. The experience highlighted the critical importance of choosing BFS over DFS for problems involving shortest paths or levels, as candidates using BFS achieved a perfect score on the problem.

Full Experience

I recently participated in an actual on-campus coding round conducted by Samsung at my college. In Round 1, we were given 3 hours to solve a single problem. The question involved a square matrix, seven types of pipes, each allowing movement in specific directions, a starting position, and the task was to compute the maximum number of reachable steps from that position through valid pipe connections.

Many of my peers attempted this problem using DFS. Out of 50 test cases, those who used DFS typically passed around 42. However, candidates who used BFS received a perfect 50/50. That round made one thing crystal clear to me: while DFS can explore, BFS guarantees correctness whenever distance, levels, or minimal steps matter. This insight led me to understand exactly why BFS worked flawlessly in that problem, and more importantly, when BFS is the right tool.

Interview Questions (1)

Q1
Max Reachable Steps in Pipe Matrix
Data Structures & AlgorithmsHard

Given a square matrix and seven distinct types of pipes, each type allowing movement in specific directions. From a designated starting position within the matrix, the task is to compute the maximum number of reachable steps through valid pipe connections. The problem implies finding the farthest reachable node in a graph where nodes are matrix cells and edges are valid pipe connections.

Discussion (0)

Share your thoughts and ask questions

Join the Discussion

Sign in with Google to share your thoughts and ask questions

No comments yet

Be the first to share your thoughts and start the discussion!