Challenge your understanding of complex data structures, recursion types, and backtracking algorithms such as N-Queens, Rat in a Maze, and Sudoku solving. This quiz is designed to test both theoretical knowledge and practical application.
Identifying Recursion Type
Which of the following code snippets exemplifies tail recursion in computing the factorial of n?
- A function that returns n * factorial(n - 1) with no accumulator.
- A function that executes factorial(n - 1) after multiplying n.
- A function that always recurses before performing multiplication.
- A function that calls itself with n - 1 and multiplies after returning.
- A function that passes an accumulator parameter and returns accumulator when n == 0.
N-Queens Constraint Checking
In the classic N-Queens problem, which condition must be checked to ensure a queen at row i, column j does not attack an already-placed queen at row k, column l?
- i == l or j == k
- i == k or j == l or |i-k| == |j-l|
- i + l == k + j
- i == j or k == l or i + j == k + l
- Only i != k and j != l
Rat in a Maze Pathfinding
Suppose you are solving the Rat in a Maze problem using backtracking; which of the following ensures all possible unique paths are explored?
- Never marking visited cells.
- Marking all unvisited neighboring cells as walls.
- Marking visited cells and unmarking them after all recursive calls from that cell are finished.
- Exploring only the right and down directions.
- Returning immediately after finding one valid path.
Recursion Stack Insight
During the solution of the Sudoku problem with backtracking, what does the recursion stack represent at any point?
- The sequence of backtracking steps from the start of the board to the current empty cell being filled.
- The collection of fully filled rows only.
- A heap of valid Sudoku configurations.
- The set of all possible numbers in the entire grid.
- The list of all empty cells left to fill in the puzzle.
Space Complexity with Recursion
What is the worst-case auxiliary space complexity of a standard recursive solution for the N-Queens problem on an n×n board?
- O(n!)
- O(1)
- O(n^2)
- O(log n)
- O(n)
Detecting Base Case Errors
In backtracking algorithms, which consequence is most likely if the base case is missing or incorrect?
- The solution will always be optimal regardless.
- All valid solutions will be skipped.
- It will simply process fewer recursive calls.
- There will be a minor reduction in performance but still correct output.
- The recursion may result in an infinite loop or stack overflow.
Head vs. Tail Recursion Output
Given a function that prints numbers from n down to 1 recursively, why does head recursion print output in descending order while tail recursion can be made to print in ascending order?
- Because both always print in descending order.
- Because tail recursion always uses loops internally.
- In head recursion, actions happen after recursion returns; in tail recursion, actions occur before the recursive call.
- Because head recursion uses more memory than tail recursion.
- Because tail recursion and head recursion are equivalent.
Efficiency in Sudoku Solvers
Which optimization is most effective in improving the efficiency of recursive backtracking Sudoku solvers?
- Checking only the first row for conflicts.
- Randomly filling empty cells first.
- Ignoring filled cells and considering only empty ones.
- Tracking used numbers in rows, columns, and subgrids using bit masks or sets.
- Trying numbers sequentially from 1 to 9 without checking constraints.
State Representation in Backtracking
When implementing backtracking for N-Queens, which data structure most efficiently represents which columns are under attack?
- A single integer for all columns.
- A matrix representing the entire board.
- A list of row indices.
- A stack containing queen objects.
- A boolean array of size n.
Pruning in Recursion
In the context of backtracking, what does the term 'pruning' specifically refer to?
- Dividing the problem into two equal halves before recursing.
- Reducing time complexity by sorting the input.
- Skipping recursive calls that cannot possibly lead to a solution based on constraints.
- Trimming whitespace from input data before recursion.
- Recursively deleting nodes in a tree data structure.