Backtracking - DSA Interview Questions
Backtracking is a powerful algorithmic technique used for solving recursive problems by exploring all possible solutions and pruning those that fail constraints. It is commonly used in combinatorial problems, such as permutations, combinations, Sudoku solving, and pathfinding. Mastering backtracking is essential for efficiently tackling complex coding interview problems.
Practice Backtracking DSA Coding Problems with Solutions
Learning Objectives:
Understand how backtracking systematically explores and prunes solution spaces to solve recursive problems. Learn to apply backtracking to problems like permutations, subset generation, graph traversal, and constraint satisfaction.
Exercise Instructions:
- Start with the first problem and attempt to solve it before checking the hint or solution.
- Ensure you understand the logic behind each solution, as this will help you with more complex problems.
- Use these exercises to reinforce your learning and identify areas that may require further study.
1. Generate All Subsets of a Set
Required Input:
[1, 2, 3]Expected Output:
[[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]
Code In Python
Run Code?
Click Run Button to view compiled output
2. Generate All Permutations of a String
Required Input:
abc'Expected Output:
['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
Code In Python
Run Code?
Click Run Button to view compiled output
3. Find All Unique Combinations That Sum to a Target
Required Input:
[2, 3, 6, 7], target=7Expected Output:
[[2, 2, 3], [7]]
Code In Python
Run Code?
Click Run Button to view compiled output
4. Find All Unique Combinations That Sum to a Target (Without Repetition)
Required Input:
[10, 1, 2, 7, 6, 1, 5], target=8Expected Output:
[[1, 1, 6], [1, 2, 5], [1, 7], [2, 6]]
Code In Python
Run Code?
Click Run Button to view compiled output
5. Word Search in a Grid
Required Input:
[['A', 'B', 'C', 'E'], ['S', 'F', 'C', 'S'], ['A', 'D', 'E', 'E']], word='ABCCED'Expected Output:
True
Code In Python
Run Code?
Click Run Button to view compiled output
6. Generate Balanced Parentheses
Required Input:
n=3Expected Output:
['((()))', '(()())', '(())()', '()(())', '()()()']
Code In Python
Run Code?
Click Run Button to view compiled output
7. Find All Palindromic Partitions of a String
Required Input:
aab'Expected Output:
[['a', 'a', 'b'], ['aa', 'b']]
Code In Python
Run Code?
Click Run Button to view compiled output
8. Find All Letter Case Permutations of a String
Required Input:
a1b2'Expected Output:
['a1b2', 'a1B2', 'A1b2', 'A1B2']
Code In Python
Run Code?
Click Run Button to view compiled output
9. Find All Unique Subsets with Duplicates
Required Input:
[1, 2, 2]Expected Output:
[[], [1], [1, 2], [1, 2, 2], [2], [2, 2]]
Code In Python
Run Code?
Click Run Button to view compiled output
10. Find All Paths in a Maze
Required Input:
[[1, 1, 0], [1, 1, 1], [0, 1, 1]]Expected Output:
[['Down', 'Right', 'Down', 'Right'], ['Down', 'Right', 'Right', 'Down'], ['Right', 'Down', 'Down', 'Right'], ['Right', 'Down', 'Right', 'Down']]
Code In Python
Run Code?
Click Run Button to view compiled output


