Subsets
medium 22 minGenerating every subset is the first backtracking pattern most interviews ask for, and there are four standard ways to write it: take-or-skip recursion, a start-index loop, iterative cascading and bitmask counting.
Where it shows up
The same pattern appears across everyday software.
Testing feature flag combinations
With a few feature flags, a test suite can run every on/off combination by counting through masks: 3 flags give 8 configurations. Because each flag doubles the count, teams with many flags switch to pairwise testing, which covers every pair of settings with far fewer runs.
Permission sets
Unix file permissions are a 3-bit mask per user class: read = 4, write = 2, execute = 1, so 7 is rwx and 5 is r-x. Role-based access control works the same way at a larger scale: a user's permissions are a subset of all permissions, often stored as bit flags and checked with a single AND.
Query optimisers
Classic database optimisers choose a join order with dynamic programming over subsets of the tables in a query: the best plan for each subset is built from the best plans of its smaller subsets. That is why the cost grows exponentially with the number of joined tables, and why optimisers fall back to heuristics past a dozen or so.
Bitmask dynamic programming
Problems such as the travelling salesman over a small number of cities, assigning n workers to n jobs, or set cover store the set of items used so far as an integer mask. Subset enumeration plus memoisation turns n! orderings into about 2ⁿ · n states.
Market basket analysis
Shops look for itemsets that are often bought together. The Apriori algorithm explores subsets of items level by level and prunes any set whose smaller subsets are already rare, the same idea as pruning a subsets search tree.