In this blog post, I will be giving a brief introduction about another important pattern for technical interview. This pattern is called Subsets.This pattern is very useful to solve problems that involve dealing with Permutations and Combinations of a given set of elements. This data structure pattern describes an efficient Breadth First Search approach to handle all the problems.
The pattern looks like this;
Let assume that we are given a set of [1,9,2]
- First step , we start with an empty set :[]
- Then, we add the first number (1) to all the existing subsets to create new subsets: [, ];
- Then, add the second number (9) to all the existing subsets: [, , , [1,9]];
- Lastly, we add the third number (2) to all the existing subsets: [, , , [1,9], , [1,2], [9,2], [1,9,2]].