Sample questions
Question 1Easy
What does this Python code print?
def f(n):
if n == 0:
return 0
return n % 10 + f(n // 10)
print(f(4567))
- 22
- 7654
- 4567
- 4
Answer: A. 22
Explanation
f returns the sum of the decimal digits: 7 + 6 + 5 + 4 = 22.
Step-by-step solution
- n % 10 is the last digit; n // 10 drops it.
- f(4567) = 7 + f(456)
= 7 + 6 + f(45)
= 7 + 6 + 5 + f(4).
- f(4) = 4 + f(0)
= 4.
- Total 7 + 6 + 5 + 4 = 22.
Why the other options are wrong
- B. That would be reversing the digits, not adding them.
- C. The function does not return n.
- D. 4 is only the last call's digit.
Tip: % 10 gets the last digit; // 10 removes it.
Question 2Medium
What is the length of the longest common subsequence of "ABCBDAB" and "BDCABA"?
- 6
- 3
- 5
- 4
Answer: D. 4
Explanation
One LCS is "BCBA" (others include "BDAB"). The DP table dp[i][j] = dp[i-1][j-1] + 1 on a match, else max(dp[i-1][j], dp[i][j-1]), gives 4.
Step-by-step solution
- Fill dp[i][j] for prefixes of ABCBDAB and BDCABA.
- On a match, dp = diagonal + 1; otherwise max(up, left).
- The bottom-right cell is 4.
- One LCS is BCBA; BDAB is another.
Why the other options are wrong
- A. 6 is the length of the shorter string.
- B. 3 misses a match; BCBA has 4 characters.
- C. No common subsequence of length 5 exists.
Tip: Match: diagonal + 1. No match: max(top, left).
Question 3Medium
Using coins of value 1, 2 and 5 (unlimited supply), how many different ways can you make the amount 5, where order does not matter?
- 3
- 4
- 9
- 5
Answer: B. 4
Explanation
5; 2+2+1; 2+1+1+1; 1+1+1+1+1. That is 4 combinations.
Step-by-step solution
- Count combinations, not orders.
- Using a 5: {5}.
- Without 5: {2,2,1}, {2,1,1,1}, {1,1,1,1,1}.
- Total 4.
Why the other options are wrong
- A. This misses one combination, often {2,1,1,1}.
- C. 9 counts different orders (permutations) of the same coins.
- D. 5 double-counts a combination.
Tip: Coins outer, amount inner: combinations. Amount outer: permutations.
Question 4Medium
What is the edit (Levenshtein) distance between "kitten" and "sitting", with insert, delete and substitute each costing 1?
- 4
- 5
- 2
- 3
Answer: D. 3
Explanation
kitten -> sitten (substitute k with s) -> sittin (substitute e with i) -> sitting (insert g).
Step-by-step solution
- kitten -> sitten: substitute k with s.
- sitten -> sittin: substitute e with i.
- sittin -> sitting: insert g.
- 3 edits, and the DP table confirms 3 is the minimum.
Why the other options are wrong
- A. One more edit than needed.
- B. Far more than the minimum.
- C. Two edits cannot fix k, e and the missing g.
Tip: Count the mismatches, then add or remove for the length difference.
Question 5Medium
You can climb 1 or 2 steps at a time. In how many distinct ways can you climb a staircase of 10 steps?
- 55
- 100
- 89
- 144
Answer: C. 89
Explanation
ways(n) = ways(n-1) + ways(n-2), with ways(1) = 1 and ways(2) = 2:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89.
Step-by-step solution
- The last move is 1 step or 2 steps.
- So ways(n) = ways(n-1) + ways(n-2).
- ways(1) = 1, ways(2) = 2, then 3, 5, 8, 13, 21, 34, 55, 89.
- ways(10) = 89.
Why the other options are wrong
- A. 55 is ways(9).
- B. 100 is not in the sequence.
- D. 144 is ways(11).
Tip: Stairs with 1 or 2 steps: ways(n) = Fib(n+1).
Question 6Medium
What is the length of the longest strictly increasing subsequence of [10, 9, 2, 5, 3, 7, 101, 18]?
- 4
- 5
- 3
- 6
Answer: A. 4
Explanation
One example is 2, 3, 7, 101 (or 2, 5, 7, 18). It can be found in O(n log n) with patience sorting.
Step-by-step solution
- Look for the longest strictly increasing subsequence.
- 2, 5, 7, 101 (or 2, 3, 7, 18) has length 4.
- Patience sorting tails: 10 -> 9 -> 2 -> [2,5] -> [2,3] -> [2,3,7] -> [2,3,7,101] -> [2,3,7,18].
- Length of the tails list = 4.
Why the other options are wrong
- B. No increasing subsequence of length 5 exists.
- C. 3 misses a longer chain such as 2, 3, 7, 18.
- D. 6 is too long.
Tip: LIS in O(n log n): keep the smallest tail for each length.
Question 7Hard
With coin denominations {1, 3, 4}, what is the minimum number of coins needed to make 6, and does the greedy "largest coin first" approach find it?
- 2 coins (3 + 3); greedy also finds it
- 2 coins (4 + 2); greedy finds it
- 2 coins (3 + 3); greedy fails and uses 3 coins
- 3 coins (4 + 1 + 1); greedy is optimal
Answer: C. 2 coins (3 + 3); greedy fails and uses 3 coins
Explanation
Greedy picks 4, then 1 + 1 = 3 coins. DP finds 3 + 3 = 2 coins. Greedy is only guaranteed optimal for special (canonical) coin systems.
Step-by-step solution
- Greedy takes the largest coin 4, leaving 2.
- Then 1 + 1: 3 coins in total.
- But 3 + 3 = 6 uses only 2 coins.
- So the optimum is 2 coins and greedy fails.
Why the other options are wrong
- A. Greedy picks 4 first, so it never finds 3 + 3.
- B. There is no coin of value 2.
- D. 3 coins is greedy's answer, which is not optimal.
Tip: Test greedy with a small counter-example before trusting it.
Question 8Hard
Which two properties must a problem have for dynamic programming to apply?
- Greedy choice property and sorted input
- Optimal substructure and overlapping subproblems
- A recursive solution with no base case
- Divide and conquer with independent subproblems only
Answer: B. Optimal substructure and overlapping subproblems
Explanation
Overlapping subproblems make memoization or tabulation useful; optimal substructure means an optimal solution is built from optimal sub-solutions. Independent subproblems (as in merge sort) suit plain divide and conquer.
Step-by-step solution
- Optimal substructure: an optimal answer is built from optimal answers to subproblems.
- Overlapping subproblems: the same subproblems recur.
- Both together make storing and reusing results (DP) pay off.
Why the other options are wrong
- A. Greedy choice is what greedy algorithms need.
- C. Recursion without a base case never ends.
- D. Independent subproblems suit divide and conquer; DP helps when they overlap.
Tip: DP = optimal substructure + overlapping subproblems.
Question 9Hard
0/1 knapsack: capacity 7, items (weight, value) = (1, 1), (3, 4), (4, 5), (5, 7). What is the maximum total value?
- 12
- 10
- 9
- 8
Answer: C. 9
Explanation
Best: weights 3 + 4 = 7 with values 4 + 5 = 9. Alternatives: 5 + 1 gives 8; 3 + 1 gives 5. Each item can be used at most once.
Step-by-step solution
- Capacity 7. Try promising combinations.
- (3,4) + (4,5): weight 7, value 9.
- (5,7) + (1,1): weight 6, value 8. (1,1) + (3,4): value 5.
- No other subset fits with more value, so 9.
Why the other options are wrong
- A. 12 would need (5,7) + (4,5) with weight 9, over capacity.
- B. 10 would need weight 8 or more.
- D. 8 is (5,7) + (1,1), not the best.
Tip: 0/1 knapsack: iterate capacity downwards so each item is used once.
Practise all 9 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →