Data Structures & AlgorithmsHardProduct Company SDE · Amazon/Microsoft-style OA

Problem: A recursive solution for 'number of ways to partition n into parts' is memoised on (n, largest part allowed). With n ≤ 1000, how many states and what total time?

  1. A. n states – O(n)
  2. B. 2ⁿ states – exponential
  3. C. n³ states – O(n³)
  4. D. About n² = 10⁶ states with O(1) transitions each – O(n²) time

Correct answer: D. About n² = 10⁶ states with O(1) transitions each – O(n²) time

State (remaining, maxPart) ranges over n × n values. With the transition p(n, k) = p(n, k − 1) + p(n − k, k), each state is O(1), giving O(n²) – about a million steps.

More Coding Interview Problems questions

Exams that ask this topic