Skip to content
CodeKruPro

All permutations of a list

Coding problemMediumAsked in: Adobe, Amazon, Apple, Google, Meta, Microsoft

Given a list of distinct integers, return every possible ordering (also called a permutation) of all its values. Each ordering uses every value exactly once.

Return the orderings in any order, but the values inside each ordering must be in that ordering's order. Each ordering must appear exactly once.

Examples

Example 1
Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Example 2
Input: nums = [0,1]
Output: [[0,1],[1,0]]
Example 3
Input: nums = [7]
Output: [[7]]

Constraints

  • The list holds between 1 and 8 integers
  • The values are distinct and between -100 and 100

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in