Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
Problem: Subset-sum (does any subset of n integers sum exactly to a target T?) has a DP solution running in O(n*T). Why is this not considered a true polynomial-time algorithm?
- A. Because T can be exponentially large relative to the input's bit length, making O(n*T) only pseudo-polynomial
- B. Because O(n*T) is always slower than O(2^n)
- C. Because subset-sum has no known DP solution at all
- D. Because DP never counts as polynomial time
Correct answer: A. Because T can be exponentially large relative to the input's bit length, making O(n*T) only pseudo-polynomial
T's magnitude isn't bounded by the number of bits needed to encode the input; as T grows exponentially in the input size, O(n*T) grows exponentially too, so it's called pseudo-polynomial, not truly polynomial.
More Coding Interview Problems questions
- Problem: What is the average-case and worst-case time complexity of the quickselect algorithm for finding the k-th smallest element?
- Problem: Which of these sorting algorithms is guaranteed O(n log n) in the worst case AND is stable (preserves the relative order of equal…
- Problem: Counting sort can sort n integers in the range [0, k] in O(n + k) time. What is the key limitation that makes it unsuitable for…
- Problem: A trie (prefix tree) storing n words with total length L supports which operation's time complexity most efficiently?
- Problem: Floyd's cycle detection ('tortoise and hare') finds whether a linked list has a cycle. What are its time and space complexity?
- Problem: What is the time complexity of a segment tree's range-query and point-update operations on an array of size n?