Unit 3: Recursion and Advanced Techniques - Practice Quiz

CSE330 — Competitive Coding Approaches-Techniques 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 What is recursion in programming?

Introduction to recursion Easy
A. A variable changing type
B. A function calling itself
C. An array sorting itself
D. A loop running forever

2 What is the main purpose of a base condition in recursion?

Base condition Easy
A. To stop recursive calls
B. To repeat calls forever
C. To declare global variables
D. To increase input size

3 Which problem is commonly solved by using the recursive relation ?

Solving problems using recursion Easy
A. Sorting an array
B. Finding a factorial
C. Counting distinct values
D. Finding an average

4 When designing a recursive solution, the original problem is usually reduced to what?

Solving problems using recursion Easy
A. A fixed infinite loop
B. A larger unrelated problem
C. A random data structure
D. A smaller similar problem

5 Which technique is a classic recursive example?

Classic and Modern Approaches Easy
A. Computing Fibonacci numbers
B. Displaying a static message
C. Changing a file extension
D. Reading a keyboard event

6 Which modern technique avoids repeatedly solving the same recursive subproblems?

Classic and Modern Approaches Easy
A. Serialization
B. Compilation
C. Memoization
D. Tokenization

7 When a function calls itself directly, what type of recursion is used?

Direct vs. Indirect Recursion Easy
A. Indirect recursion
B. Mutual iteration
C. Direct recursion
D. Nested iteration

8 Function A calls function B, and function B calls function A. What does this demonstrate?

Direct vs. Indirect Recursion Easy
A. Tail iteration
B. Linear searching
C. Indirect recursion
D. Direct recursion

9 In tail recursion, where does the recursive call appear?

Tailed vs. Non-Tailed Recursion Easy
A. Inside the function name
B. After the return completes
C. As the final operation
D. Before every condition

10 Which statement describes non-tail recursion?

Tailed vs. Non-Tailed Recursion Easy
A. Work remains after the recursive call
B. Only one value is returned
C. The function has no parameters
D. No recursive call is performed

11 Where are active recursive function calls generally stored?

Memory Allocation in Recursion Easy
A. Program counter
B. Source file
C. Input buffer
D. Call stack

12 What error may occur when recursion creates too many active calls?

Memory Allocation in Recursion Easy
A. File overflow
B. Type mismatch
C. Stack overflow
D. Syntax error

13 What is a common advantage of recursion?

Advantages & disadvantages of recursive programming Easy
A. It guarantees faster execution
B. It removes every base case
C. It can simplify tree traversal
D. It always uses constant memory

14 What is a common disadvantage of recursive programming?

Advantages & disadvantages of recursive programming Easy
A. It cannot process numbers
B. It cannot return a result
C. It requires global variables
D. It may use extra stack memory

15 What does a backtracking algorithm usually do after a choice leads to an invalid solution?

Backtracking Easy
A. Accept the choice and terminate
B. Repeat the same choice forever
C. Delete the entire input
D. Undo the choice and try another

16 How many permutations are possible for three distinct elements?

Permutations Easy
A.
B.
C.
D.

17 What is the goal of the Combination Sum problem?

Combination Sum Easy
A. Find the largest array element
B. Find substrings with equal lengths
C. Find combinations matching a target sum
D. Find permutations in sorted order

18 In the N-Queens problem, which queens attack each other?

N-Queens Easy
A. Queens placed on different diagonals
B. Queens placed two rows apart
C. Queens sharing a row, column, or diagonal
D. Queens sharing only the same corner

19 How is the next value generated while checking whether a number is happy?

Next happy number Easy
A. Add the squares of its digits
B. Add the cubes of its digits
C. Multiply the squares of its digits
D. Multiply all of its digits

20 What property must the numeric parts of a sum string satisfy?

Sum string Easy
A. Each later part contains the same repeated digit
B. Each later part equals the previous two parts' sum
C. Each later part equals the previous part's square
D. Each later part is smaller than every earlier part

21 Consider the recursive function:

F(n) = 1 when n = 0, and F(n) = n + F(n - 1) otherwise.

What value is returned by F(4)?

Introduction to recursion Medium
A.
B.
C.
D.

22 A recursive function is intended to compute for non-negative integers:

power(a, n) = a * power(a, n - 1)

Which base condition makes the function correct?

Base condition Medium
A. If n == 1, return 1
B. If a == 0, return n
C. If n == 0, return 1
D. If n == 0, return a

23 A recursive binary search is called on a sorted array of elements. In the worst case, approximately how many times can the search range be halved before it becomes empty?

Solving problems using recursion Medium
A.
B.
C.
D.

24 A direct recursive Fibonacci implementation repeatedly solves the same subproblems. Which technique most directly improves it while preserving its recursive structure?

Classic and Modern Approaches Medium
A. Randomizing the call order
B. Memoizing computed values
C. Increasing the stack size
D. Replacing integers with strings

25 Function A calls function B, function B calls function C, and function C calls function A. What type of recursion does this call structure represent?

Direct vs. Indirect Recursion Medium
A. Indirect recursion
B. Direct recursion
C. Tree recursion
D. Tail recursion

26 Which function demonstrates direct recursion?

Direct vs. Indirect Recursion Medium
A. A(n) calls B(n - 1), which calls A(n - 2)
B. solve(n) calls solve(n - 1)
C. main() calls solve(n) exactly once
D. solve(n) calls helper(n - 1)

27 Which recursive return statement is tail-recursive, assuming acc stores the partial result?

Tailed vs. Non-Tailed Recursion Medium
A. return fact(n - 1, n * acc)
B. return n * fact(n - 1)
C. return 1 + fact(n - 1)
D. return fact(n - 1) + n

28 Why is return n * factorial(n - 1) classified as non-tail recursion?

Tailed vs. Non-Tailed Recursion Medium
A. The function has only one recursive call
B. Multiplication remains after the recursive call
C. The argument decreases by exactly one
D. The base case returns a constant value

29 A recursive function makes one call with n - 1 until n == 0, and each call stores only a constant amount of local data. What is its auxiliary stack-space complexity?

Memory Allocation in Recursion Medium
A.
B.
C.
D.

30 For traversing a very deep tree, what is the main practical risk of using recursive depth-first search instead of an explicit stack?

Advantages & disadvantages of recursive programming Medium
A. Every node requires two visits
B. The call stack may overflow
C. The traversal becomes breadth-first
D. The tree must be stored twice

31 A backtracking algorithm places values into positions and discovers that the current partial assignment violates a constraint. What should it do next?

Backtracking Medium
A. Undo the latest choice and try another
B. Accept the partial assignment as a solution
C. Restart with the same initial choice
D. Continue until a complete assignment forms

32 Which change most effectively prunes a backtracking search without removing valid solutions?

Backtracking Medium
A. Delay every constraint check until the final level
B. Duplicate each recursive branch before exploring
C. Reject partial states that already violate constraints
D. Generate every complete candidate before checking

33 How many distinct permutations can be formed from the characters in AABC?

Permutations Medium
A.
B.
C.
D.

34 In a swap-based recursive permutation algorithm, what must happen after returning from the recursive call for a chosen position?

Permutations Medium
A. Swap the elements back to restore the state
B. Advance directly to the base condition
C. Delete the chosen element permanently
D. Sort the remaining suffix before continuing

35 Given candidates [2, 3, 6, 7], where each candidate may be reused, which set contains all unique combinations whose sum is ?

Combination Sum Medium
A. [[2, 3], [7]]
B. [[2, 2, 3], [7]]
C. [[2, 2, 2], [3, 7]]
D. [[2, 2, 3], [3, 3]]

36 In a Combination Sum backtracking algorithm where candidates may be reused, which recursive index should be passed after choosing candidate i?

Combination Sum Medium
A. i - 1, revisiting earlier candidates
B. i + 1, forbidding the same candidate
C. 0, restarting every branch
D. i, allowing the same candidate again

37 When placing a queen at row r and column c, which previously placed queen creates a diagonal conflict?

N-Queens Medium
A. A queen at (r - 3, c + 1)
B. A queen at (r - 1, c + 2)
C. A queen at (r - 2, c + 2)
D. A queen at (r - 2, c + 1)

38 A row-by-row N-Queens solver stores occupied columns and diagonals in sets. Which pair of expressions can identify the two diagonals of position (r, c)?

N-Queens Medium
A. r * c and r + c
B. r / c and r % c
C. r - c and r * c
D. r - c and r + c

39 A happy number repeatedly replaces a number with the sum of the squares of its digits until reaching . What is the smallest happy number greater than ?

Next happy number Medium
A.
B.
C.
D.

40 A sum string can be split into numbers such that every number after the first two equals the sum of the previous two. Which split correctly proves that 122436 is a sum string?

Sum string Medium
A. 12, 2, 24, 36
B. 12, 24, 36
C. 1, 22, 43, 6
D. 1, 2, 24, 36

41 Consider a recursive procedure that makes two calls on an input of half the original size and performs constant local work: , with . Assuming no memoization, which pair correctly describes its total running time and maximum recursion depth?

Introduction to recursion Hard
A. time and depth
B. time and depth
C. time and depth
D. time and depth

42 A function uses if (n == 1) return; as its only base condition and otherwise calls itself with n / 2 using integer division. Which replacement guarantees termination for every integer input?

Base condition Hard
A. if (n < 0) return;
B. if (n == 0) return;
C. if (n % 2 == 0) return;
D. if (n <= 1) return;

43 A power function computes an even exponent using power(x, n/2) * power(x, n/2) without storing the first result. For restricted to powers of two, what are its time complexity and recursion depth?

Solving problems using recursion Hard
A. time and depth
B. time and depth
C. time and depth
D. time and depth

44 A language has no tail-call optimization, but a deeply recursive state machine must avoid native stack overflow while preserving recursive control flow. Which transformation best achieves this?

Classic and Modern Approaches Hard
A. Memoize every call by its complete argument tuple
B. Inline only the base cases into the caller function
C. Return thunks and execute them through a trampoline loop
D. Replace each call with a macro-expanded function body

45 Consider A(n) = 0 for and otherwise A(n) = 1 + B(n-1). Also, B(n) = 0 for and otherwise B(n) = 1 + A(\lfloor n/2 \rfloor). What are the value returned by A(20) and the maximum number of simultaneously active calls, including the base-case call?

Direct vs. Indirect Recursion Hard
A. Return value and maximum calls
B. Return value and maximum calls
C. Return value and maximum calls
D. Return value and maximum calls

46 Which implementation is tail-recursive, assuming ordinary eager evaluation?

Tailed vs. Non-Tailed Recursion Hard
A. gcd(a, b) = b == 0 ? a : gcd(b, a % b)
B. sum(n) = n == 0 ? 0 : sum(n - 1) + n
C. height(t) = t == null ? 0 : 1 + height(t.left)
D. fact(n) = n == 0 ? 1 : n * fact(n - 1)

47 Quicksort is modified to recurse only on the smaller partition and process the larger partition by updating bounds inside a loop. What worst-case bounds does this guarantee for auxiliary call-stack space and running time?

Memory Allocation in Recursion Hard
A. stack space and time
B. stack space and time
C. stack space and time
D. stack space and time

48 A depth-first traversal explores a tree with branching factor and maximum depth . Which statement accurately compares a recursive implementation with an iterative implementation using an explicit stack?

Advantages & disadvantages of recursive programming Hard
A. Both need traversal state, but recursion can overflow the call stack
B. Recursion needs state, while iteration always needs state
C. Recursion needs state, while iteration always needs state
D. Both need traversal state because every node remains active

49 A combination-search algorithm sorts candidates and stops its loop when currentSum + candidate[i] > target. Under which condition is this pruning rule logically valid?

Backtracking Hard
A. All previously chosen candidates are positive and distinct
B. All remaining candidates are distinct and nondecreasing
C. All remaining candidates are nonnegative and nondecreasing
D. The target is positive and the input contains no duplicates

50 A backtracking solver builds assignments for variables, each with possible values. A feasibility test costing is executed at every recursion-tree node. With no effective pruning, what is the tightest asymptotic upper bound on running time?

Backtracking Hard
A.
B.
C.
D.

51 For the multiset , how many distinct full-length permutations exist, and which loop condition correctly prevents duplicate generation after sorting?

Permutations Hard
A. ; skip when i > 0 && a[i] == a[i-1] && used[i-1]
B. ; skip when i > 0 && a[i] == a[i-1] && !used[i-1]
C. ; skip whenever i > 0 && a[i] == a[i-1]
D. ; skip when i > 0 && a[i] == a[i-1] && !used[i-1]

52 Using one-based indexing, what is the nd lexicographic permutation of ?

Permutations Hard
A. 25413
B. 24531
C. 24513
D. 25134

53 Candidates are $[2,3,5]`, each may be reused any number of times, and combinations differing only by order are identical. How many combinations sum to $10$?

Combination Sum Hard
A.
B.
C.
D.

54 A recursive Combination Sum solver allows unlimited reuse by recursing with the same candidate index. Which input property can make the recursion nonterminating even when the target is finite?

Combination Sum Hard
A. The candidates are stored in descending order
B. The target is smaller than every candidate
C. The candidate list contains repeated positive values
D. The candidate list contains the value

55 In a bitmask N-Queens solver, cols, diagL, and diagR mark columns attacked in the current row, and full has its lowest bits set. Which expression computes available positions, and how are diagonal masks advanced after choosing bit p?

N-Queens Hard
A. full ^ (cols | diagL | diagR); shift both updated diagonals right
B. full | ~(cols & diagL & diagR); shift both updated diagonals left
C. full & (cols | diagL | diagR); shift left diagonal right and right diagonal left
D. full & ~(cols | diagL | diagR); shift (diagL | p) left and (diagR | p) right

56 On a board with zero-based indices, queens have been placed at , , and . Which columns remain legal for a queen in row ?

N-Queens Hard
A. Columns and
B. Columns and
C. Columns and
D. Columns and

57 A happy number repeatedly replaces a number by the sum of the squares of its decimal digits until reaching or entering a cycle. What is the smallest happy number strictly greater than ?

Next happy number Hard
A.
B.
C.
D.

58 A sum string can be partitioned into at least three decimal numbers such that every number after the first two equals the sum of its two predecessors. For 199100199, which initial pair produces a valid complete partition?

Sum string Hard
A. 19 and 9
B. 1 and 99
C. 199 and 100
D. 1 and 9

59 When testing all possible first and second numbers of a sum string, which validation rule correctly handles leading zeros and termination?

Sum string Hard
A. Reject every chosen number beginning with 0, and accept after matching any single generated sum
B. Reject any multi-digit chosen number beginning with 0, and accept only after consuming the entire string
C. Normalize chosen numbers by deleting zeros, and accept when the final generated sum is nonzero
D. Allow leading zeros in chosen numbers, and accept after consuming at least half of the string

60 Identical glasses of capacity are arranged in rows. Overflow from each glass is divided equally between the two glasses directly below it. If units are poured into the top glass, how much water is retained in the glass at row , column , using zero-based indices?

Water overflow Hard
A.
B.
C.
D.