Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Every ordering of a list

    MediumAbout 25 minFull AccessLearn it: Permutations and Combination Sum: undo and pruning

    The task

    A test planner wants to run a few setup steps in every possible order, to catch steps that only work in one order.

    Complete Permutations(int[] values). The values are distinct. Return all n! orderings of them, each as its own list; the orderings may come in any order (the tests sort the list of orderings). Leave the input array as it was: if you swap values in place, swap them back. The array is never null.

    Signature

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

    Examples

    • [1, 2] → [[1, 2], [2, 1]]. Two values have 2! = 2 orderings.
    • [] → [[]]. The empty list has one ordering, the empty one.

    Limits: Up to 8 values (40,320 orderings).

    The tests check: an empty list, one, two and three values, negative values, that every ordering holds every value, that your method leaves the input unchanged, 5 and 7 values, and 8 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
      {
          // All n! orderings of the distinct values, each its own list; the input array is not changed.
          public static List<List<int>> Permutations(int[] values)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.