How do you decide between BFS and DFS in production code?

Beyond interview answers, traversal choice affects memory, observability, and failure behavior. What practical constraints drive your choice?

Hari

BFS when the nearest match or shortest unweighted path matters, DFS when memory is tight or I want a cheap early walk, but in production I also care about failure shape: a bad wide fan-out can crush BFS queues while deep recursive DFS can hide stack bugs, so I often use iterative DFS with a depth cap.

stack = [(root, 0)]
while stack:
    node, d = stack.pop()
    if d > max_depth: continue
    for child in reversed(node.children):
        stack.append((child, d + 1))

BayMax