Challenge - 5 Problems
Recursion Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Factorial Function
What is the output of the following Java code that calculates factorial using recursion?
DSA Java
public class Factorial { public static int factorial(int n) { if (n == 0) return 1; return n * factorial(n - 1); } public static void main(String[] args) { System.out.println(factorial(4)); } }
Attempts:
2 left
💡 Hint
Factorial of 4 is 4 * 3 * 2 * 1
✗ Incorrect
The recursive function multiplies n by factorial of n-1 until n is 0, where it returns 1. So factorial(4) = 4*3*2*1 = 24.
🧠 Conceptual
intermediateWhy Recursion Can Express Problems Loops Cannot
Which of the following problems is best solved using recursion rather than loops?
Attempts:
2 left
💡 Hint
Think about problems with hierarchical or nested structures.
✗ Incorrect
Binary trees have a nested structure that recursion naturally fits. Loops alone cannot easily traverse all branches without extra data structures.
🔧 Debug
advancedIdentify the Error in Recursive Fibonacci Code
What error will this Java code produce when run?
DSA Java
public class Fibonacci { public static int fib(int n) { if (n == 0 || n == 1) return n; return fib(n - 1) + fib(n - 2); } public static void main(String[] args) { System.out.println(fib(-1)); } }
Attempts:
2 left
💡 Hint
Check the base case conditions and input values.
✗ Incorrect
The base case only handles n == 0 or n == 1, but negative input causes infinite recursion because fib(n-1) and fib(n-2) keep decreasing without stopping.
🚀 Application
advancedOutput of Recursive Directory Listing Simulation
Given this Java code simulating recursive directory listing, what is the printed output?
DSA Java
import java.util.*; public class Directory { String name; List<Directory> subDirs; public Directory(String name) { this.name = name; this.subDirs = new ArrayList<>(); } public void addSubDir(Directory d) { subDirs.add(d); } public void printDirs() { System.out.println(name); for (Directory d : subDirs) { d.printDirs(); } } public static void main(String[] args) { Directory root = new Directory("root"); Directory home = new Directory("home"); Directory user = new Directory("user"); Directory docs = new Directory("docs"); root.addSubDir(home); home.addSubDir(user); user.addSubDir(docs); root.printDirs(); } }
Attempts:
2 left
💡 Hint
The printDirs method prints current directory then recursively prints subdirectories in order.
✗ Incorrect
The method prints the current directory name, then recursively calls printDirs on each subdirectory in the order they were added.
🧠 Conceptual
expertWhy Some Recursive Problems Cannot Be Translated Easily to Loops
Which statement best explains why some recursive problems cannot be easily rewritten as loops?
Attempts:
2 left
💡 Hint
Think about how recursion keeps track of each call's state.
✗ Incorrect
Recursion uses the call stack to remember each nested call's state. Loops alone do not have this built-in mechanism and need extra structures to simulate it.
