Data Structures & AlgorithmsModerateTCS NQT · Infosys

Consider the bitmask DP that counts subsets of {1..n} with sum equal to S. For n = 4 and S = 5, what does the DP answer, counting the empty subset as a subset of sum 0?

  1. A. 2
  2. B. 1
  3. C. 3
  4. D. 4

Correct answer: A. 2

Subsets of {1,2,3,4} with sum 5 are {1,4} and {2,3}, which is 2. The trap values 1, 3 and 4 come from counting only one of the two subsets, from counting single-element subsets, or from reading the wrong bit.

More Coding Interview Problems questions

Exams that ask this topic