Depth-First Search (DFS): Interview Questions and Practice Problems
2 min readSep 1, 2018
A Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. One starts at the root (selecting some arbitrary node as the root in the case of a graph) and explores as far as possible along each branch before backtracking.
Above graph shows order in which the nodes are discovered in DFS. Here’re the list of commonly asked interview questions that can be solved using DFS –
- Depth First Search (DFS) | Iterative & Recursive Implementation
- Generate list of possible words from a character matrix
- Find length of longest path in the matrix with consecutive characters
- Replace all occurrences of 0 that are completely surrounded by 1 in a binary matrix
- Replace all occurrences of 0 that are not surrounded by 1 in a binary matrix
- Find all occurrences of given string in a character matrix
- Find path from source to destination in a matrix that satisfies given constraints
- Flood Fill Algorithm
- Traverse given directory using BFS and DFS in Java
- Types of edges involved in DFS and relation between them
- Arrival and Departure Time of Vertices in DFS
- Find longest path in a Directed Acyclic Graph (DAG)
- Find the path between given vertices in a directed graph
- Determine if a graph is Bipartite Graph using DFS
- Check if Graph is Strongly Connected or not using one DFS Traversal
- Find Cost of Shortest Path in DAG using one pass of Bellman-Ford
- Check if given Graph is Strongly Connected or not
- Check if given digraph is a DAG (Directed Acyclic Graph) or not
- 2-Vertex Connectivity in the graph
- 2-Edge Connectivity in a Graph
- Determine if an undirected graph is a Tree (Acyclic Connected Graph)
- Check if an undirected graph contains cycle or not
- Transitive Closure of a Graph
- Topological Sort Algorithm for DAG using DFS
- Find correct order of alphabets in a given dictionary of ancient origin
- Find first k maximum occurring words in given set of strings
- Find maximum occurring word in given set of strings
- Lexicographic sorting of given set of keys
- Construct a directed graph from undirected graph that satisfies given constraints
- Inorder Tree Traversal | Iterative & Recursive
- Preorder Tree Traversal | Iterative & Recursive
- Postorder Tree Traversal | Iterative & Recursive
Thank you.