Backtracking: Practice Problems and Interview Questions
Backtracking is a general algorithm for finding all (or some) solutions to some computational problems, notably constraint satisfaction problems, that incrementally builds candidates to the solutions, and abandons each partial candidate `c` (“backtracks”) as soon as it determines that `c` cannot possibly be completed to a valid solution.
Backtracking can be applied only for problems that admit the concept of a “partial candidate solution” and a relatively quick test of whether it can be completed to a valid solution. Backtracking is often much faster than brute force enumeration of all candidates since it can eliminate a large number of candidates with a single test.
In this post, we have listed out common problems that can be solved using the backtracking technique:
- Print all possible solutions to N–Queens problem
- Print all possible Knight’s tours on a chessboard
- Find the shortest path in a maze
- Find the longest possible route in a matrix
- Find the path from source to destination in a matrix that satisfies given constraints
- Find the total number of unique paths in a maze from source to destination
- Find all combinations of elements satisfying given constraints
- K–Partition Problem | Printing all partitions
- Magnet Puzzle
- Print all k–colorable configurations of a graph (Vertex coloring of a graph)
- Find the minimum number possible by doing at-most `k` swaps
- Find all permutations of a string — C++, Java, Python
- Determine whether a string matches with a given pattern
- Find all paths from the first cell to the last cell of a matrix
- Print all shortest routes in a rectangular grid
- Find all distinct combinations of a given length with repetition allowed
- Print all combinations of numbers from 1 to `n` having sum `n`
- Print all triplets in an array with a sum less than or equal to a given number
- Find all possible combinations by replacing given digits with characters of the corresponding list
- Find all combinations of non-overlapping substrings of a string
- Find all n-digit numbers with equal sum of digits at even and odd indices
- Find all occurrences of the given string in a character matrix
- Print all distinct subsets of a given set
- Find all binary strings that can be formed from a wildcard pattern
- Find ways to calculate a target from elements of the specified array
- Find all Possible Topological Orderings of a DAG
- Print all Hamiltonian paths present in a graph
- Find the path between given vertices in a directed graph
- Generate a list of possible words from a character matrix
- Print all paths from the root to leaf nodes of a binary tree
- Print all paths from leaf to root node of a binary tree
- Generate the power set of a given set
Thank You.