Challenge - 5 Problems
Backtracking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Backtracking Subset Sum Example
What is the output of the following Java code that uses backtracking to find subsets summing to 5?
DSA Java
import java.util.*; public class SubsetSum { static void findSubsets(int[] nums, int target, int index, List<Integer> current, List<List<Integer>> result) { if (target == 0) { result.add(new ArrayList<>(current)); return; } if (target < 0 || index == nums.length) { return; } // Include nums[index] current.add(nums[index]); findSubsets(nums, target - nums[index], index + 1, current, result); // Exclude nums[index] current.remove(current.size() - 1); findSubsets(nums, target, index + 1, current, result); } public static void main(String[] args) { int[] nums = {2, 3, 5}; List<List<Integer>> result = new ArrayList<>(); findSubsets(nums, 5, 0, new ArrayList<>(), result); for (List<Integer> subset : result) { System.out.println(subset); } } }
Attempts:
2 left
💡 Hint
Think about all subsets of {2,3,5} that add up exactly to 5.
✗ Incorrect
The code explores all subsets using backtracking. It finds two subsets that sum to 5: [2, 3] and [5].
🧠 Conceptual
intermediateWhy Greedy Fails for Subset Sum
Why does a greedy approach fail to solve the subset sum problem correctly in all cases?
Attempts:
2 left
💡 Hint
Consider if picking the largest number first always works.
✗ Incorrect
Greedy algorithms pick the best immediate option but can miss combinations that require skipping some choices to reach the target sum.
❓ Predict Output
advancedOutput of Greedy Coin Change Example
What is the output of this Java code using a greedy approach to make change for 30 cents with coins {25, 10, 5}?
DSA Java
import java.util.*; public class GreedyCoinChange { public static void main(String[] args) { int[] coins = {25, 10, 5}; int amount = 30; List<Integer> result = new ArrayList<>(); for (int coin : coins) { while (amount >= coin) { amount -= coin; result.add(coin); } } System.out.println(result); } }
Attempts:
2 left
💡 Hint
Greedy picks the largest coin first until it can't.
✗ Incorrect
The greedy approach picks 25 first, then 5 to make 30 cents.
🧠 Conceptual
advancedBacktracking vs Greedy: Which Solves N-Queens?
Why is backtracking suitable for solving the N-Queens problem but greedy is not?
Attempts:
2 left
💡 Hint
Think about the need to undo choices when conflicts arise.
✗ Incorrect
Backtracking tries all placements and backtracks on conflicts, ensuring a valid solution. Greedy cannot undo wrong choices.
🔧 Debug
expertIdentify the Output of Backtracking Maze Solver
What is the printed path output of this Java backtracking maze solver code?
DSA Java
import java.util.*; public class MazeSolver { static int[][] maze = { {1, 0, 0, 0}, {1, 1, 0, 1}, {0, 1, 0, 0}, {1, 1, 1, 1} }; static int N = 4; static List<String> path = new ArrayList<>(); static boolean solve(int x, int y) { if (x == N - 1 && y == N - 1) { path.add("(" + x + "," + y + ")"); return true; } if (x >= 0 && y >= 0 && x < N && y < N && maze[x][y] == 1) { maze[x][y] = -1; // mark visited path.add("(" + x + "," + y + ")"); if (solve(x + 1, y)) return true; if (solve(x, y + 1)) return true; path.remove(path.size() - 1); maze[x][y] = 1; // unmark } return false; } public static void main(String[] args) { if (solve(0, 0)) { System.out.println(path); } else { System.out.println("No path found"); } } }
Attempts:
2 left
💡 Hint
The solver tries down first, then right.
✗ Incorrect
The solver moves down when possible, then right. It finds the path through (0,0),(1,0),(1,1),(2,1),(3,1),(3,2),(3,3).
