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?

  1. A. O(n log n)
  2. B. O(n^2), bounded by the number of substrings
  3. C. Exponential, O(2^n), since every position may or may not start a new partition
  4. 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

Exams that ask this topic