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?
- A. 4ⁿ
- B. n · 2ⁿ
- C. 2ⁿ · 2ⁿ / 2
- 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
- Problem: A dynamic array doubles its capacity when full (starting at capacity 1), copying all elements to the new storage. Count element…
- Problem: A Rabin–Karp search uses a weak hash (the sum of character codes) and verifies every hash hit. Count true matches and spurious…
- Problem: Find the longest proper prefix of s that is also a suffix (the 'happy prefix'). python def happy(s): p = [0] len(s); k = 0 for i…
- Problem: Generate the 'look-and-say' sequence starting from '1'. python def nxt(s): out, i = [], 0 while i < len(s): j = i while j <…
- Problem: Print a matrix in spiral order. python def spiral(m): res = [] while m: res += m.pop(0) m = [list(r) for r in zip(m)][::-1]…
- Problem: Run Conway's Game of Life on an unbounded board (a live cell survives with 2 or 3 live neighbours; a dead cell becomes live with…