Skip to content
MediumBacktrackingAI interview only

Permutations

Asked atmetaamazongooglemicrosoftbloomberglinkedinapple

01 · Problem

Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

02 · Examples

Example 01
Input
nums = [1,2,3]
Output
[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

All 6 permutations of [1,2,3] are listed.

Example 02
Input
nums = [0,1]
Output
[[0,1],[1,0]]

There are 2 permutations of a 2-element array.

Example 03
Input
nums = [1]
Output
[[1]]

A single element has exactly one permutation.

03 · Constraints

  • 011 <= nums.length <= 6
  • 02-10 <= nums[i] <= 10
  • 03All the integers of nums are unique

04 · Optimal complexity

Time
O(n! * n)
Space
O(n!)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.