Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Every subset of a list

    MediumAbout 25 minFull AccessLearn it: Subsets: the cleanest first example

    The task

    A pricing tool tries every combination of optional add-ons on a product, from no add-on at all to all of them.

    Complete AllSubsets(int[] values). The values are distinct. Return every subset of them, the empty one included, so 2^n lists for n values. The subsets may come in any order, and so may the values inside a subset (the tests sort before comparing). Every returned list must be its own list object: changing one must not change another. The array is never null.

    Signature

    public static List<List<int>> AllSubsets(int[] values)

    Examples

    • [1, 2] → [[], [1], [1, 2], [2]]. Each value is either in or out: 2 × 2 = 4 subsets.
    • [] → [[]]. The empty list has one subset, the empty one.

    Limits: Up to 16 values (65,536 subsets).

    The tests check: an empty list, one, two and three values, negative values, the empty subset, that every subset is its own list, 10 values, and 16 values in time.

    Hints

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public static class Solution
      {
          // Every subset of the distinct values (2^n lists), each its own list.
          public static List<List<int>> AllSubsets(int[] values)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.