Register for our webinar

How to Nail your next Technical Interview

1 hour
Enter details
Select webinar slot
*Invalid First Name
*Invalid Last Name
*Invalid Phone Number
By sharing your contact details, you agree to our privacy policy.
Select your webinar time
Step 1
Step 2
You have registered for our webinar
Oops! Something went wrong while submitting the form.
Enter details
Select webinar slot
Step 1
Step 2
You are scheduled with Interview Kickstart.
Oops! Something went wrong while submitting the form.
About usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar
Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career

Generate All Subsets Of A Set Problem

Generate All Subsets Of A Set Problem Statement

Generate ALL possible subsets of a given set. The set is given in the form of a string s containing distinct lowercase characters 'a' - 'z'.


"s": "xy"


["", "x", "y", "xy"]


  • Any set is a subset of itself.
  • Empty set is a subset of any set.
  • Output contains ALL possible subsets of given string.
  • Order of strings in the output does not matter. E.g. s = "a", arrays ["", "a"] and ["a", ""] both will be accepted.
  • Order of characters in any subset must be same as in the input string. For s = "xy", array ["", "x", "y", "xy"] will be accepted, but ["", "x", "y", "yx"] will not be accepted.


  • 0 <= length of s <= 19
  • s only contains distinct lowercase English letters.

We have provided 2 solutions:

  • Recursive Solution : other_solution.cpp.
  • Iterative Solution : Generate All Subsets Of A Set Solution: Optimal.

Have a look at both the solutions.


Both solutions are valid, but the recursive solution is slightly slower because of function calls and variable passing.

Also you should observe that the number of subsets will always be a power of 2, specifically 2n, where n represents the length of string s.

Time Complexity

O(2n * n).

As we will generate 2n strings of length O(n).

Auxiliary Space Used

O(2n * n).

As we will store 2n strings of length O(n) in the output array to be returned.

Space Complexity

O(2n * n).

As auxiliary space used is O(2n * n) and input is O(n), hence O(2n * n) + O(n) = O(2n * n).

Code For Generate All Subsets Of A Set Solution: Optimal

Asymptotic complexity in terms of the length of \`s\` \`n\`:
* Time: O(2^n * n).
* Auxiliary space: O(2^n * n).
* Total space: O(2^n * n).

vector<string> generate_all_subsets(string &s) {
    int n = s.length();
    vector<string> all_subsets;
    // Base case when n = 0 i.e. s = "".
    Suppose s = "xyz".
    Now try to find pattern in following steps (think how any step is related to previous step!):
    - [""]
    - ["", "x"]
    - ["", "x", "y", "xy"]
    - ["", "x", "y", "xy", "z", "xz", "yz", "xyz"]
    Let me explain what we have done in last step.
    First take array from previous step, that is ["", "x", "y", "xy"], append 'z' to each string,
    that is ["z", "xz", "yz", "xyz"], now merge it with array in previous step!
    for (int i = 1; i <= n; i++) {
        int old_len = all_subsets.size();
        for (int j = 0; j < old_len; j++) {
            Note that we are doing push_back that is adding value at the end of array, so we
            already have the old values stored in it.
            all_subsets.push_back(all_subsets[j] + s[i - 1]);
    return all_subsets;

We hope that these solutions to generate all subsets of a set problem have helped you level up your coding skills. You can expect problems like these at top tech companies like Amazon and Google.

If you are preparing for a tech interview at FAANG or any other Tier-1 tech company, register for Interview Kickstart's FREE webinar to understand the best way to prepare.

Interview Kickstart offers interview preparation courses taught by FAANG+ tech leads and seasoned hiring managers. Our programs include a comprehensive curriculum, unmatched teaching methods, and career coaching to help you nail your next tech interview.

We offer 18 interview preparation courses, each tailored to a specific engineering domain or role, including the most in-demand and highest-paying domains and roles, such as:

‍To learn more, register for the FREE webinar.

Try yourself in the Editor

Note: Input and Output will already be taken care of.

Recommended Posts

All Posts