Challenge - 5 Problems
Backtracking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Backtracking Path Exploration
What is the output of the following C code that uses backtracking to print all subsets of {1, 2}?
DSA C
#include <stdio.h> void backtrack(int arr[], int n, int index, int subset[], int subset_size) { if (index == n) { printf("{"); for (int i = 0; i < subset_size; i++) { printf("%d", subset[i]); if (i < subset_size - 1) printf(", "); } printf("}\n"); return; } // Include current element subset[subset_size] = arr[index]; backtrack(arr, n, index + 1, subset, subset_size + 1); // Exclude current element backtrack(arr, n, index + 1, subset, subset_size); } int main() { int arr[] = {1, 2}; int subset[2]; backtrack(arr, 2, 0, subset, 0); return 0; }
Attempts:
2 left
💡 Hint
Think about the order in which the backtracking includes and excludes elements.
✗ Incorrect
The backtracking first explores the path excluding all elements, printing the empty set {}. Then it includes elements in order, printing {1}, {2}, and finally {1, 2}.
🧠 Conceptual
intermediate1:30remaining
Number of Nodes in Backtracking Decision Tree
For a backtracking algorithm that explores all subsets of a set with 3 elements, how many nodes are there in the decision tree?
Attempts:
2 left
💡 Hint
Each element can be either included or excluded, forming a binary tree.
✗ Incorrect
For 3 elements, the decision tree is a full binary tree with height 3. It has 2^(3+1) - 1 = 15 nodes.
🔧 Debug
advanced2:00remaining
Identify the Backtracking Logic Error
What error does the following C code produce when trying to generate permutations of {1, 2}?
DSA C
#include <stdio.h> void permute(int arr[], int start, int end) { if (start == end) { for (int i = 0; i <= end; i++) printf("%d ", arr[i]); printf("\n"); } else { for (int i = start; i <= end; i++) { int temp = arr[start]; arr[start] = arr[i]; arr[i] = temp; permute(arr, start + 1, end); // Missing swap back here } } } int main() { int arr[] = {1, 2}; permute(arr, 0, 1); return 0; }
Attempts:
2 left
💡 Hint
Check if the array is restored after recursive calls.
✗ Incorrect
The code swaps elements but does not swap them back after recursion, causing the array to remain changed and producing duplicate permutations.
❓ Predict Output
advanced2:30remaining
Output of Backtracking with Pruning
What is the output of this C code that uses backtracking with pruning to find subsets of {1, 2, 3} with sum less than 4?
DSA C
#include <stdio.h> void backtrack(int arr[], int n, int index, int subset[], int subset_size, int current_sum) { if (current_sum >= 4) return; if (index == n) { printf("{"); for (int i = 0; i < subset_size; i++) { printf("%d", subset[i]); if (i < subset_size - 1) printf(", "); } printf("}\n"); return; } // Include current element subset[subset_size] = arr[index]; backtrack(arr, n, index + 1, subset, subset_size + 1, current_sum + arr[index]); // Exclude current element backtrack(arr, n, index + 1, subset, subset_size, current_sum); } int main() { int arr[] = {1, 2, 3}; int subset[3]; backtrack(arr, 3, 0, subset, 0, 0); return 0; }
Attempts:
2 left
💡 Hint
Subsets with sum 4 or more are skipped.
✗ Incorrect
Subsets like {2, 3} and {1, 2, 3} have sums 5 and 6 respectively, so they are pruned and not printed.
🧠 Conceptual
expert2:00remaining
Backtracking Tree Height and Leaf Count
In a backtracking algorithm generating all permutations of n distinct elements, what is the height of the decision tree and how many leaf nodes does it have?
Attempts:
2 left
💡 Hint
Each level fixes one element in the permutation.
✗ Incorrect
The decision tree height equals the number of elements n, and the leaf nodes represent all permutations, which are n!.