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. An array sorting itself
B. A loop running forever
C. A variable changing type
D. A function calling itself

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

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

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

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

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

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

5 Which technique is a classic recursive example?

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

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

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

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

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

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

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

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

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

10 Which statement describes non-tail recursion?

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

11 Where are active recursive function calls generally stored?

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

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

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

13 What is a common advantage of recursion?

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

14 What is a common disadvantage of recursive programming?

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

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

Backtracking Easy
A. Repeat the same choice forever
B. Delete the entire input
C. Accept the choice and terminate
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 permutations in sorted order
C. Find combinations matching a target sum
D. Find substrings with equal lengths

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

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

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

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

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

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

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 n == 0, return a
C. If a == 0, return n
D. If n == 0, return 1

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. Memoizing computed values
B. Increasing the stack size
C. Replacing integers with strings
D. Randomizing the call order

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. Tail recursion
C. Tree recursion
D. Direct recursion

26 Which function demonstrates direct recursion?

Direct vs. Indirect Recursion Medium
A. solve(n) calls helper(n - 1)
B. main() calls solve(n) exactly once
C. A(n) calls B(n - 1), which calls A(n - 2)
D. solve(n) calls solve(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 1 + fact(n - 1)
C. return n * 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 base case returns a constant value
D. The argument decreases by exactly one

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. The call stack may overflow
B. Every node requires two visits
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. Continue until a complete assignment forms
B. Undo the latest choice and try another
C. Accept the partial assignment as a solution
D. Restart with the same initial choice

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. Generate every complete candidate before checking
C. Reject partial states that already violate constraints
D. Duplicate each recursive branch before exploring

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. Sort the remaining suffix before continuing
B. Delete the chosen element permanently
C. Advance directly to the base condition
D. Swap the elements back to restore the state

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], [3, 3]]
C. [[2, 2, 3], [7]]
D. [[2, 2, 2], [3, 7]]

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 - 2, c + 2)
C. A queen at (r - 2, c + 1)
D. A queen at (r - 1, c + 2)

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. Return thunks and execute them through a trampoline loop
B. Replace each call with a macro-expanded function body
C. Memoize every call by its complete argument tuple
D. Inline only the base cases into the caller function

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. fact(n) = n == 0 ? 1 : n * fact(n - 1)
B. height(t) = t == null ? 0 : 1 + height(t.left)
C. sum(n) = n == 0 ? 0 : sum(n - 1) + n
D. gcd(a, b) = b == 0 ? a : gcd(b, a % b)

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. Recursion needs state, while iteration always needs state
B. Both need traversal state because every node remains active
C. Both need traversal state, but recursion can overflow the call stack
D. Recursion needs state, while iteration always needs state

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 nonnegative and nondecreasing
C. All remaining candidates are distinct 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 when i > 0 && a[i] == a[i-1] && used[i-1]
D. ; skip whenever i > 0 && a[i] == a[i-1]

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

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

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 candidate list contains repeated positive values
C. The target is smaller than every candidate
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 9
C. 1 and 99
D. 199 and 100

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. Normalize chosen numbers by deleting zeros, and accept when the final generated sum is nonzero
B. Reject every chosen number beginning with 0, and accept after matching any single generated sum
C. Reject any multi-digit chosen number beginning with 0, and accept only after consuming the entire string
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.