Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
Question 1 of 60 · Introduction to recursion · Easy
What is recursion in programming?
Not quite. Try another option.
Answer: A function calling itself
Recursion occurs when a function calls itself to solve a smaller instance of the same problem.
Question 2 of 60 · Base condition · Easy
What is the main purpose of a base condition in recursion?
Not quite. Try another option.
Answer: To stop recursive calls
A base condition ends the recursion when a simple or final case is reached.
Question 3 of 60 · Solving problems using recursion · Easy
Which problem is commonly solved by using the recursive relation ?
Not quite. Try another option.
Answer: Finding a factorial
The factorial of can be defined recursively using , with .
Question 4 of 60 · Solving problems using recursion · Easy
When designing a recursive solution, the original problem is usually reduced to what?
Not quite. Try another option.
Answer: A smaller similar problem
A recursive solution repeatedly reduces a problem to smaller instances of the same problem.
Question 5 of 60 · Classic and Modern Approaches · Easy
Which technique is a classic recursive example?
Not quite. Try another option.
Answer: Computing Fibonacci numbers
The Fibonacci sequence is a classic example because each value can be defined using earlier values recursively.
Question 6 of 60 · Classic and Modern Approaches · Easy
Which modern technique avoids repeatedly solving the same recursive subproblems?
Not quite. Try another option.
Answer: Memoization
Memoization stores previously computed results so repeated recursive subproblems can be answered efficiently.
Question 7 of 60 · Direct vs. Indirect Recursion · Easy
When a function calls itself directly, what type of recursion is used?
Not quite. Try another option.
Answer: Direct recursion
Direct recursion occurs when a function contains a call to itself.
Question 8 of 60 · Direct vs. Indirect Recursion · Easy
Function A calls function B, and function B calls function A. What does this demonstrate?
Not quite. Try another option.
Answer: Indirect recursion
Indirect recursion occurs when functions call one another in a cycle.
Question 9 of 60 · Tailed vs. Non-Tailed Recursion · Easy
In tail recursion, where does the recursive call appear?
Not quite. Try another option.
Answer: As the final operation
In tail recursion, the recursive call is the last operation performed by the function.
Question 10 of 60 · Tailed vs. Non-Tailed Recursion · Easy
Which statement describes non-tail recursion?
Not quite. Try another option.
Answer: Work remains after the recursive call
In non-tail recursion, the function must perform another operation after the recursive call returns.
Question 11 of 60 · Memory Allocation in Recursion · Easy
Where are active recursive function calls generally stored?
Not quite. Try another option.
Answer: Call stack
Each active recursive call creates a stack frame on the call stack.
Question 12 of 60 · Memory Allocation in Recursion · Easy
What error may occur when recursion creates too many active calls?
Not quite. Try another option.
Answer: Stack overflow
Excessive recursion can exhaust the available call-stack memory and cause a stack overflow.
Question 13 of 60 · Advantages & disadvantages of recursive programming · Easy
What is a common advantage of recursion?
Not quite. Try another option.
Answer: It can simplify tree traversal
Recursion can express naturally recursive structures, such as trees, in clear and compact code.
Question 14 of 60 · Advantages & disadvantages of recursive programming · Easy
What is a common disadvantage of recursive programming?
Not quite. Try another option.
Answer: It may use extra stack memory
Recursive calls create stack frames, so recursion may consume more memory than an iterative solution.
Question 15 of 60 · Backtracking · Easy
What does a backtracking algorithm usually do after a choice leads to an invalid solution?
Not quite. Try another option.
Answer: Undo the choice and try another
Backtracking reverses an unsuccessful choice and explores a different available choice.
Question 16 of 60 · Permutations · Easy
How many permutations are possible for three distinct elements?
Not quite. Try another option.
Answer:
Three distinct elements have permutations.
Question 17 of 60 · Combination Sum · Easy
What is the goal of the Combination Sum problem?
Not quite. Try another option.
Answer: Find combinations matching a target sum
Combination Sum searches for combinations of candidate values whose total equals a given target.
Question 18 of 60 · N-Queens · Easy
In the N-Queens problem, which queens attack each other?
Not quite. Try another option.
Answer: Queens sharing a row, column, or diagonal
A valid N-Queens arrangement prevents any two queens from sharing a row, column, or diagonal.
Question 19 of 60 · Next happy number · Easy
How is the next value generated while checking whether a number is happy?
Not quite. Try another option.
Answer: Add the squares of its digits
A happy-number check repeatedly replaces a number with the sum of the squares of its digits.
Question 20 of 60 · Sum string · Easy
What property must the numeric parts of a sum string satisfy?
Not quite. Try another option.
Answer: Each later part equals the previous two parts' sum
In a sum string, each part after the first two must equal the sum of the two preceding parts.
Question 21 of 60 · Introduction to recursion · Medium
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)?
Not quite. Try another option.
Answer:
The calls compute only if . However, applying the definition directly gives , so none of the listed options would be correct.
Question 22 of 60 · Base condition · Medium
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?
Not quite. Try another option.
Answer: If n == 0, return 1
Since , returning 1 when n == 0 terminates the recursion and correctly supports every non-negative exponent.
Question 23 of 60 · Solving problems using recursion · Medium
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?
Not quite. Try another option.
Answer:
Each call halves the range, and . Therefore, about halvings are required.
Question 24 of 60 · Classic and Modern Approaches · Medium
A direct recursive Fibonacci implementation repeatedly solves the same subproblems. Which technique most directly improves it while preserving its recursive structure?
Not quite. Try another option.
Answer: Memoizing computed values
Memoization stores previously computed Fibonacci values, reducing the time complexity from exponential to .
Question 25 of 60 · Direct vs. Indirect Recursion · Medium
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?
Not quite. Try another option.
Answer: Indirect recursion
The recursion returns to A through other functions, so the recursive cycle is indirect rather than a self-call within one function.
Question 26 of 60 · Direct vs. Indirect Recursion · Medium
Which function demonstrates direct recursion?
Not quite. Try another option.
Answer: solve(n) calls solve(n - 1)
Direct recursion occurs when a function invokes itself without passing through another function.
Question 27 of 60 · Tailed vs. Non-Tailed Recursion · Medium
Which recursive return statement is tail-recursive, assuming acc stores the partial result?
Not quite. Try another option.
Answer: return fact(n - 1, n * acc)
The recursive call is the final operation. No multiplication, addition, or other work remains after the call returns.
Question 28 of 60 · Tailed vs. Non-Tailed Recursion · Medium
Why is return n * factorial(n - 1) classified as non-tail recursion?
Not quite. Try another option.
Answer: Multiplication remains after the recursive call
The current call must wait for factorial(n - 1) and then multiply its result by , so the recursive call is not the final operation.
Question 29 of 60 · Memory Allocation in Recursion · Medium
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?
Not quite. Try another option.
Answer:
There can be active stack frames, each using constant space, so the total auxiliary space is .
Question 30 of 60 · Advantages & disadvantages of recursive programming · Medium
For traversing a very deep tree, what is the main practical risk of using recursive depth-first search instead of an explicit stack?
Not quite. Try another option.
Answer: The call stack may overflow
A highly unbalanced or deep tree can create more active recursive calls than the runtime call stack supports.
Question 31 of 60 · Backtracking · Medium
A backtracking algorithm places values into positions and discovers that the current partial assignment violates a constraint. What should it do next?
Not quite. Try another option.
Answer: Undo the latest choice and try another
Backtracking abandons an invalid partial solution, restores the previous state, and explores another available choice.
Question 32 of 60 · Backtracking · Medium
Which change most effectively prunes a backtracking search without removing valid solutions?
Not quite. Try another option.
Answer: Reject partial states that already violate constraints
An invalid partial state cannot lead to a valid complete solution, so rejecting it early safely reduces the search space.
Question 33 of 60 · Permutations · Medium
How many distinct permutations can be formed from the characters in AABC?
Not quite. Try another option.
Answer:
There are arrangements, but the two A characters are identical. Thus the number is .
Question 34 of 60 · Permutations · Medium
In a swap-based recursive permutation algorithm, what must happen after returning from the recursive call for a chosen position?
Not quite. Try another option.
Answer: Swap the elements back to restore the state
Restoring the previous arrangement allows subsequent branches to start from the correct state and generate the remaining permutations.
Question 35 of 60 · Combination Sum · Medium
Given candidates [2, 3, 6, 7], where each candidate may be reused, which set contains all unique combinations whose sum is ?
Not quite. Try another option.
Answer: [[2, 2, 3], [7]]
The valid sums are and . The other listed combinations either do not sum to or omit a valid result.
Question 36 of 60 · Combination Sum · Medium
In a Combination Sum backtracking algorithm where candidates may be reused, which recursive index should be passed after choosing candidate i?
Not quite. Try another option.
Answer: i, allowing the same candidate again
Passing i keeps the selected candidate available in the next recursive call, which permits repeated use while maintaining a consistent order.
Question 37 of 60 · N-Queens · Medium
When placing a queen at row r and column c, which previously placed queen creates a diagonal conflict?
Not quite. Try another option.
Answer: A queen at (r - 2, c + 2)
Two positions share a diagonal when the absolute row difference equals the absolute column difference. Here both differences are .
Question 38 of 60 · N-Queens · Medium
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)?
Not quite. Try another option.
Answer: r - c and r + c
Cells on one diagonal have equal , while cells on the other diagonal have equal .
Question 39 of 60 · Next happy number · Medium
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 ?
Not quite. Try another option.
Answer:
For , the sequence begins , then , and finally . None of , , or reaches .
Question 40 of 60 · Sum string · Medium
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?
Not quite. Try another option.
Answer: 12, 24, 36
The split is valid because , and the entire string is consumed by the sequence.
Question 41 of 60 · Introduction to recursion · Hard
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?
Not quite. Try another option.
Answer: time and depth
The recursion tree has total nodes, while each root-to-leaf path repeatedly halves and therefore has depth .
Question 42 of 60 · Base condition · Hard
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?
Not quite. Try another option.
Answer: if (n <= 1) return;
Inputs such as never reach , and negative inputs may eventually become . The condition handles all such terminal values.
Question 43 of 60 · Solving problems using recursion · Hard
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?
Not quite. Try another option.
Answer: time and depth
Recomputing the half-power creates the recurrence , giving time. Each individual call path still has logarithmic depth.
Question 44 of 60 · Classic and Modern Approaches · Hard
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?
Not quite. Try another option.
Answer: Return thunks and execute them through a trampoline loop
A trampoline repeatedly invokes returned thunks from an iterative loop, representing continuation state without growing the native call stack.
Question 45 of 60 · Direct vs. Indirect Recursion · Hard
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?
Not quite. Try another option.
Answer: Return value and maximum calls
The calls are A(20), B(19), A(9), B(8), A(4), B(3), A(1), and B(0). Seven non-base calls contribute each.
Question 46 of 60 · Tailed vs. Non-Tailed Recursion · Hard
Which implementation is tail-recursive, assuming ordinary eager evaluation?
Not quite. Try another option.
Answer: gcd(a, b) = b == 0 ? a : gcd(b, a % b)
The recursive gcd call is the final operation. The other functions must still perform multiplication or addition after their recursive calls return.
Question 47 of 60 · Memory Allocation in Recursion · Hard
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?
Not quite. Try another option.
Answer: stack space and time
Recursing on the smaller side bounds nested calls by . Poor pivots can still produce quadratic total partitioning work.
Question 48 of 60 · Advantages & disadvantages of recursive programming · Hard
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?
Not quite. Try another option.
Answer: Both need traversal state, but recursion can overflow the call stack
Depth-first search retains a path and pending traversal information proportional to depth. Recursive calls use the bounded native call stack, whereas an explicit stack can usually grow on the heap.
Question 49 of 60 · Backtracking · Hard
A combination-search algorithm sorts candidates and stops its loop when currentSum + candidate[i] > target. Under which condition is this pruning rule logically valid?
Not quite. Try another option.
Answer: All remaining candidates are nonnegative and nondecreasing
If later candidates cannot be smaller or negative, every later choice also exceeds the target. Negative or unsorted remaining values could make a later branch feasible.
Question 50 of 60 · Backtracking · Hard
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?
Not quite. Try another option.
Answer:
The full search tree contains nodes for fixed up to a constant geometric factor. An test at each node gives time.
Question 51 of 60 · Permutations · Hard
For the multiset , how many distinct full-length permutations exist, and which loop condition correctly prevents duplicate generation after sorting?
Not quite. Try another option.
Answer: ; skip when i > 0 && a[i] == a[i-1] && !used[i-1]
The count is . At one recursion level, a duplicate is skipped when its identical predecessor has not been selected in the current partial permutation.
Question 52 of 60 · Permutations · Hard
Using one-based indexing, what is the nd lexicographic permutation of ?
Not quite. Try another option.
Answer: 24531
Using zero-based rank , factorial blocks select indices from the remaining ordered elements, producing 24531.
Question 53 of 60 · Combination Sum · Hard
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$?
Not quite. Try another option.
Answer:
The combinations are , , , and . Maintaining a nondecreasing candidate index avoids counting reordered copies.
Question 54 of 60 · Combination Sum · Hard
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?
Not quite. Try another option.
Answer: The candidate list contains the value
Selecting leaves both the remaining target and candidate index unchanged, so the recursive state repeats forever unless zero is rejected or specially handled.
Question 55 of 60 · N-Queens · Hard
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?
Not quite. Try another option.
Answer: full & ~(cols | diagL | diagR); shift (diagL | p) left and (diagR | p) right
Available bits are those inside the board and absent from every attack mask. Diagonal attacks move one column in opposite directions on the next row.
Question 56 of 60 · N-Queens · Hard
On a board with zero-based indices, queens have been placed at , , and . Which columns remain legal for a queen in row ?
Not quite. Try another option.
Answer: Columns and
Used columns eliminate , while diagonals into row additionally eliminate and reinforce . Only columns and remain.
Question 57 of 60 · Next happy number · Hard
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 ?
Not quite. Try another option.
Answer:
reaches the unhappy cycle through , and reaches it through . For , the first transformation gives , followed by .
Question 58 of 60 · Sum string · Hard
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?
Not quite. Try another option.
Answer: 1 and 99
The partition is , satisfying and while consuming the entire string.
Question 59 of 60 · Sum string · Hard
When testing all possible first and second numbers of a sum string, which validation rule correctly handles leading zeros and termination?
Not quite. Try another option.
Answer: Reject any multi-digit chosen number beginning with 0, and accept only after consuming the entire string
A single 0 is valid, but representations such as 01 are not. Every generated sum must match consecutively, and validation succeeds only when the whole string is consumed.
Question 60 of 60 · Water overflow · Hard
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?
Not quite. Try another option.
Answer:
The uncapped amounts reaching row are . Their overflows send to row , column .
That's unit 3
Answer a few questions and your score shows up here.
Nearly through. Did unit 3 of CSE330 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 3 of CSE330 save you a night before the exam?
Then you can help keep it free for the next batch. Ads cover part of the server bill;
the rest comes out of a student's own pocket: the domain, the storage, and keeping
the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason.
to try again.