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?

  1. A. It can be done in O(n) time since there are only n elements
  2. 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
  3. C. It always requires O(n^2) time
  4. 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

Exams that ask this topic