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?
- A. n states – O(n)
- B. 2ⁿ states – exponential
- C. n³ states – O(n³)
- 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
- Problem: A function has T(n) = T(n − 1) + T(n − 2) + O(1) without memoisation. What is its growth?
- Problem: Dijkstra is run with a binary heap but WITHOUT the 'skip stale entry' check (if d > dist[u]: continue). What happens?
- Problem: n ≤ 10⁵ points on a line; answer 'how many points lie in [l, r]?' for 10⁵ queries. What is the simplest efficient solution?
- Problem: Implement a queue using two stacks and report the dequeued values. python class Q: def init(self): self.inb, self.out = [], []…
- Problem: Evaluate a basic calculator expression with +, −, parentheses and spaces. python def calc(s): res, num, sign, st = 0, 0, 1, []…
- Problem: Decode a string like 3[a2[c]] where k[...] repeats the bracket content k times. python def decode(s): st, cur, k = [], '', 0 for…