0
0
DSA Cprogramming~20 mins

Backtracking Concept and Decision Tree Visualization in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Backtracking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A
{}
{1}
{2}
{1, 2}
B
{}
{2}
{1}
{1, 2}
C
{1}
{1, 2}
{2}
{}
D
{1, 2}
{1}
{2}
{}
Attempts:
2 left
💡 Hint
Think about the order in which the backtracking includes and excludes elements.
🧠 Conceptual
intermediate
1: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?
A8
B15
C16
D7
Attempts:
2 left
💡 Hint
Each element can be either included or excluded, forming a binary tree.
🔧 Debug
advanced
2: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;
}
ACompilation error due to missing semicolon
BCauses infinite recursion
CPrints no output
DPrints duplicate permutations
Attempts:
2 left
💡 Hint
Check if the array is restored after recursive calls.
Predict Output
advanced
2: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;
}
A
{}
{3}
{2}
{2, 3}
{1}
{1, 3}
{1, 2}
{1, 2, 3}
B
{}
{1}
{2}
{3}
{1, 2}
{1, 3}
{2, 3}
C
{}
{1}
{2}
{3}
{1, 2}
{1, 3}
D
{}
{1}
{2}
{1, 2}
{3}
Attempts:
2 left
💡 Hint
Subsets with sum 4 or more are skipped.
🧠 Conceptual
expert
2: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?
AHeight = n, Leaf nodes = n!
BHeight = n!, Leaf nodes = n
CHeight = n-1, Leaf nodes = n^2
DHeight = 2^n, Leaf nodes = 2^n
Attempts:
2 left
💡 Hint
Each level fixes one element in the permutation.