Data Structures & AlgorithmsHardAmazon · Microsoft coding round
Problem: A recursive solution explores every possible way to partition a string into palindromic substrings (palindrome partitioning). Without memoization, what is its worst-case time complexity for a string of length n?
- A. O(n log n)
- B. O(n^2), bounded by the number of substrings
- C. Exponential, O(2^n), since every position may or may not start a new partition
- D. O(n), since each character is visited once
Correct answer: C. Exponential, O(2^n), since every position may or may not start a new partition
At each position the recursion can choose to cut or not cut, leading to up to 2^(n-1) distinct partitions in the worst case (e.g. a string of all identical characters), making the brute-force search exponential without memoization.
More Coding Interview Problems questions
- Problem: Which best describes the relationship between the number of valid Binary Search Trees on n distinct keys and the Catalan numbers?
- Problem: What is the time complexity of the standard iterative binary search, and what precondition must hold for it to work correctly?
- Problem: Which best describes why DFS-based topological sort (using finish times, pushing to a stack) correctly produces a topological…
- Problem: A problem requires finding all pairs in an array that sum to a target, allowing duplicate pairs to be counted once. Which is the…
- Problem: Which statement about the time complexity of building a suffix array naively (sorting all n suffixes with standard string…
- Problem: A delivery app logs the parcel weights of a day in an array. Return the indices of the two parcels whose weights add up exactly…