Data Structures & AlgorithmsModerateTCS NQT Digital · Product Company SDE
Problem: A DP solution for 'minimum coins for amount A using c denominations' with A ≤ 10⁴ and c ≤ 100 is accepted. A friend proposes plain recursion min over coins without memoisation. What is the friend's worst-case complexity?
- A. Exponential – roughly O(c^(A / smallest coin)) because subproblems are recomputed
- B. O(A · c), the same as the DP
- C. O(A log c)
- D. O(c²)
Correct answer: A. Exponential – roughly O(c^(A / smallest coin)) because subproblems are recomputed
Without memoisation the recursion tree branches c ways at each level and is up to A levels deep, recomputing the same amounts many times. Memoising on the amount collapses it to A·c states and transitions, about 10⁶ here.
More Coding Interview Problems questions
- Problem: Find the largest square of 1s in a binary matrix and return its area. python def square(m): R, C = len(m), len(m[0]); dp = [[0]…
- Problem: Buy and sell a stock any number of times with a one-day cooldown after each sale. Maximise profit. python def cooldown(p): hold…
- Problem: Count the number of distinct ways to reach target t by putting + or − in front of every number. python from functools import…
- Problem: Generate all subsets of a set of feature flags (backtracking) and count those whose sum equals a target. python def count(a, t…
- Problem: Generate all permutations of a list that may contain duplicates, without repeats. How many are produced? python def perms(a)…
- Problem: Generate all valid strings of n pairs of brackets in lexicographic order of the recursion ('(' tried first). python def gen(n)…