The Number of Ways to Build Sturdy Brick Wall problem asks you to count ways to tile a wall of width width and height height using bricks of varying lengths (always fitting within width). Adjacent rows cannot have any shared seam positions (except at the ends). This coding problem uses bitmask DP over valid row patterns.
Microstrategy and Google ask this because it requires precomputing all valid single-row layouts (as bitmasks of seam positions), then counting arrangements of height rows where no two consecutive rows share a seam bit. The array, bitmask, bit manipulation, and dynamic programming interview pattern is comprehensively tested.
Bitmask DP with compatible row precomputation. Generate all valid row patterns (bitmasks of seam positions) by recursively placing bricks. Build a compatibility matrix: two row patterns are compatible if (pattern1 & pattern2) == 0 (no shared seams). dp[i][p] = ways to have the first i rows with the i-th row being pattern p. Transition: sum over compatible patterns.
width=4, height=2. Bricks=[1,2]. Valid rows: [4-brick], [2+2], [1+3?], [2+1+1], [1+1+2], [1+1+1+1]. Represent seams as bitmasks (excluding ends). Compatible pairs: no shared seam bit. Count all compatible pairs of rows for height=2.
(a & b) == 0, not XOR).Tiling DP with bitmask representation is a powerful technique for grid problems where rows interact via shared boundaries. The key steps: (1) generate all valid row bitmasks, (2) precompute compatible pairs, (3) apply DP. Step 2 can be optimized by precomputing compatibility lists. Practice brick wall tiling and similar tiling problems where each row's pattern must be compatible with adjacent rows.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Find the Minimum Cost Array Permutation | Hard | Solve | |
| Maximum AND Sum of Array | Hard | Solve | |
| Smallest Sufficient Team | Hard | Solve | |
| Concatenated Divisibility | Hard | Solve | |
| Minimum Time to Kill All Monsters | Hard | Solve |