Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE
Problem: Which statement about the backtracking solution to generating all subsets of an n-element set is correct?
- A. It can be done in O(n) time since there are only n elements
- B. It must produce 2^n subsets, so any correct algorithm (iterative or recursive) takes at least O(2^n) time to enumerate them all
- C. It always requires O(n^2) time
- D. Subsets can be generated in O(log n) using binary search
Correct answer: B. It must produce 2^n subsets, so any correct algorithm (iterative or recursive) takes at least O(2^n) time to enumerate them all
Each of the n elements independently is in or out of a subset, giving exactly 2^n subsets; simply writing out that many subsets already costs at least O(2^n), regardless of the generation technique.
More Coding Interview Problems questions
- Problem: Generating all permutations of an n-element array via backtracking takes how much time, and why?
- Problem: In the N-Queens backtracking search, what pruning technique keeps the search from degenerating into a full O(N^N) brute force?
- Problem: Which best explains why a monotonic stack is the standard technique for the 'next greater element' problem, giving O(n) overall…
- Problem: A sliding window maximum over an array of size n with window size k is computed with a monotonic deque. What is its time…
- Problem: Two Sum on an unsorted array can be solved in O(n) using a hash map, or in O(n log n) using sort-and-two-pointers. Why might the…
- Problem: Three Sum (find all triplets summing to zero) is commonly solved by sorting the array and then applying two pointers for each…