Possible subsets of a set of distinct integers

This problem asks us to return all possible subsets of a given set of distinct integers. For example, if the input is [1,2,3], then the output should be all possible subsets of those integers: [1], [1,2], [1,2,3], [1,3], [2], [2,3], and [3].

Problem

Given a set of distinct integers, return all possible subsets.
Input: [1,2,3]
Output:
[
  [1],
  [1,2],
  [1,2,3],
  [1,3],
  [2],
  [2,3],
  [3],
]

Solution

This solution is in C#. Our A.I. can create solutions in multiple languages.
by kayakane
using System;
using System.Collections.Generic;

namespace InterviewPreperationGuide.Core.CrackingTheCodingInterview.c16q8 {
    public class Solution {
        public void Init () {
            Console.WriteLine ();
        }

        // Time: O (2^n)
        // Space: O (n)
        public List<List<int>> GetSubsets (List<int> set, int index) {
            List<List<int>> allSubsets = null;

            if (set.Count == index) {
                allSubsets = new List<List<int>> ();
                allSubsets.Add (new List<int> ());
            } else {
                allSubsets = GetSubsets (set, index + 1);
                int item = set[index];
                List<List<int>> moreSubsets = new List<List<int>> ();

                foreach (List<int> subset in allSubsets) {
                    List<int> newSubset = new List<int> ();
                    newSubset.AddRange (subset);
                    newSub set.Add (item);
                    moreSubsets.Add (newSubset);
                }

                allSubsets.AddRange (moreSubsets);
            }

            return allSubsets;
        }
    }
}

A.I. Evaluation of the Solution

The candidate's solution is complete and solves the problem. The approach is to generate all subsets by iterating through the input array and adding each element to all existing subsets.

Evaluated at: 2022-12-12 14:15:41