Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard

A bitmask DP loops over every mask of n bits and, for each mask, over all of its submasks. What is the total number of (mask, submask) pairs visited?

  1. A. 4ⁿ
  2. B. n · 2ⁿ
  3. C. 2ⁿ · 2ⁿ / 2
  4. D. 3ⁿ

Correct answer: D. 3ⁿ

Each bit independently is in neither, in the mask only, or in both (a submask bit must also be in the mask): three choices per bit, so 3ⁿ pairs. That is why submask-enumeration DPs (e.g. set partition) run in O(3ⁿ), not O(4ⁿ).

More Coding Interview Problems questions

Exams that ask this topic