# Permutations — Aced

Aced (formerly Exponent) publishes a public search API over its indexed pages; the guide is at https://www.aced.io/agents/agent-access.

Source: https://www.aced.io/questions/2212/leetcode-medium-permutations-dp
Source observed: 2026-11-05T00:08:12.633Z
Last checked: 2026-11-05T00:08:12.633+00:00
Indexed: 2026-11-05T21:02:54.456+00:00

---

# Permutations

**Difficulty:** Medium  
**Category:** Data Structures & Algorithms; Coding

We provide this coding-interview question for practicing permutation generation. Given an array of distinct integers, return **all possible permutations** of the array. A permutation is a rearrangement of the array’s elements.

## Example

```text
Input: nums = [1, 2, 3]
Output: [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
```

## Question details

The question is associated with six reported interviews. The rendered page shows the following roles and companies:

- Roles: [Software Engineer](https://www.aced.io/questions?role=swe), [Site Reliability Engineer](https://www.aced.io/questions?role=sre), [Data Engineer](https://www.aced.io/questions?role=data-engineer), and [Data Scientist](https://www.aced.io/questions?role=data-science)
- Companies: [TikTok](https://www.aced.io/questions?company=tiktok), [Amazon](https://www.aced.io/questions?company=amazon), [Apple](https://www.aced.io/questions?company=apple), [Adobe](https://www.aced.io/questions?company=adobe), [LinkedIn](https://www.aced.io/questions?company=linkedin), and [Google](https://www.aced.io/questions?company=google)
- The rendered details show it was last asked 2 months ago, relative to the page observation timestamp of 2026-11-05.

The page includes Question, Solution, and Community tabs. The rendered page shows eight community entries, while the supplied content includes four answers.

## Community answers

### Amruta M. — Python

```python
from typing import List

def permute(nums: List[int]) -> List[List[int]]:
    result = []
    used = [False]*len(nums)
    def backtrack(index, path):
        if index == len(nums):
            result.append(path[:])
            return
        for start in range(len(nums)):
            if used[start]:
                continue
            path.append(nums[start])
            used[start] = True
            backtrack(index+1, path)
            path.pop()
            used[start] = False
    backtrack(0, [])
    return result

# debug your code below
print(permute([1]))
```

Amruta M.’s profile: [Aced member](https://www.aced.io/users/a2491678-8451-4249-9f54-57d569ace941). Karma: [201](https://www.aced.io/karma "201 karma"). Posted September 28, 2026.

### Harsh K. — Java

```java
import java.util.*;

class Solution {
    public static void swap(int[] nums, int a, int b){
        int temp = nums[a];
        nums[a] = nums[b];
        nums[b] = temp;
    }

    public static void solve(int[] nums, List<int[]> ans, int idx){
        //base case
        if(idx >= nums.length){
            ans.add(nums.clone());
            return;
        }
        for(int i = idx ;i< nums.length;i++){
            swap(nums, idx, i);
            solve(nums, ans, idx + 1);
            swap(nums, idx, i);
        }
    }

    public static List<List<Integer>> permute(int[] nums) {
        List<int[]> ans = new ArrayList<>();
        solve(nums, ans, 0);
        List<List<Integer>> res = new ArrayList<>();
        for(int[] arr : ans){
            List<Integer> temp = new ArrayList<>();
            for(int a : arr){
                temp.add(a);
            }
            res.add(temp);
        }
        return res;
    }

    public static void main(String[] args) {
        // debug your code below
        int[] arr = {1, 2, 3};
        List<List<Integer>> result = permute(arr);
        System.out.println(result);
    }
}
```

Harsh K.’s profile: [https://www.aced.io/users/7d5e3f38-56ac-483f-aef8-20eff34ef735](https://www.aced.io/users/7d5e3f38-56ac-483f-aef8-20eff34ef735). Karma: [575](https://www.aced.io/karma "575 karma"). Posted August 4, 2026.

### Ansen K. — Python

```python
from typing import List

def permute(nums: List[int]) -> List[List[int]]:
    if len(nums) <= 1:
        return [nums]
    result = []
    first, rest = nums[0], nums[1:]
    rest_result = permute(rest)
    for res in rest_result:
        for i in range(len(res)+1):
            new_res = res[:i] + [first] + res[i:]
            result.append(new_res)
    return result

# debug your code below
print(permute([1, 2, 3]))
```

Ansen K.’s profile: [Aced member](https://www.aced.io/users/a194ea06-8f3d-4293-8d72-44827ac9ecde). Karma: [132](https://www.aced.io/karma "132 karma"). Posted September 22, 2026.

### Shubham S. — C++

```cpp
#include <vector>
#include <iostream>
using namespace std;

vector<vector<int>> res;

void fun(int i, vector<int> nums){
    if(i == nums.size()){
        res.push_back(nums);
        return;
    }
    for(int j=i; j<nums.size(); j++){
        swap(nums[i], nums[j]);
        fun(i+1, nums);
        swap(nums[i], nums[j]);
    }
}

vector<vector<int>> permute(vector<int>& nums) {
    // your code goes here
    res.clear();
    int n = nums.size();
    // if(n == 0 || n == 1){
    // return [nums];
    // }
    fun(0, nums);
    return res;
}

// comment out before running tests
int main() {
    vector<int> nums = {1, 2, 3};
    vector<vector<int>> result = permute(nums);
    for (const auto& perm : result) {
        for (int num : perm) {
            cout << num << " ";
        }
        cout << endl;
    }
    return 0;
}
```

Shubham S.’s profile: [https://www.aced.io/users/10120557-f114-492e-ba09-e66c321cf590](https://www.aced.io/users/10120557-f114-492e-ba09-e66c321cf590). Karma: [80](https://www.aced.io/karma "80 karma"). Posted September 26, 2026.

### Brighton A. — Python

```python
from typing import List

def permute(nums: List[int]) -> List[List[int]]:
    res, sol = [], []
    n = len(nums)
    def backtrack():
        if len(sol) == n:
            res.append(sol[:])
            return
        for i in range(n):
            if nums[i] not in sol:
                sol.append(nums[i])
                backtrack()
                sol.pop()
    backtrack()
    return res

# debug your code below
print(permute([1]))
```

Brighton A.’s profile: [https://www.aced.io/users/e0c6fd86-38e4-4071-ba97-754f2577a824](https://www.aced.io/users/e0c6fd86-38e4-4071-ba97-754f2577a824). Karma: [164](https://www.aced.io/karma "164 karma"). Posted May 14, 2026.

## Interview experiences

We include two shared interview experiences:

1. [TikTok — Site Reliability Engineer, Entry Level / L3](https://www.aced.io/experiences/tiktok-site-reliability-engineer-interview-591119), shown as 2 months ago:
   > “I would say the phone call with recruiter is just discussion on the role and scheduling the coding interview. The coding interview is on hackerrank and 1 - 2 leetcode medium problems.”
2. [Amazon — Software Engineer, Intern](https://www.aced.io/experiences/amazon-software-engineer-intern-interview-bccbb0), shown as 2 years ago:
   > “Recruiter then oa then had an interview with hiring manager and then another with someone on the team both technical leetcodes”

## Related question

- [Find all permutations of a given string.](https://www.aced.io/questions/1148/all-permutations-given-string)

## Related courses

### [Data Science Interview Prep](https://www.aced.io/courses/data-science)

![Data Science Interview Prep](https://images.ctfassets.net/x78yjrjc11pq/3U4C5C0OkDR0UeGch4AmlE/7ad489430b676a088cb973307fe77884/Data_Science_Interview_Prep_Course.jpeg?w=800&h=800&fm=webp&q=75)

We help candidates prepare for data science roles at Google, Amazon, Microsoft, Meta, Apple, and other companies through mock interviews, frameworks, and advice from senior candidates. Topics include statistics, experimentation, coding, SQL, machine learning, behavioral interviews, and more.

### [Data Engineering Interview Prep](https://www.aced.io/courses/data-engineering)

![Data Engineering Interview Prep](https://images.ctfassets.net/x78yjrjc11pq/5XoOCgXlGqN2fa1zaroNWd/9a2e23df48a212bc00d352873feb2d6a/Data_Engineer_Interview_Prep_Hero_Image.jpg?w=800&h=800&fm=webp&q=75)

We help candidates prepare for data engineering roles at Meta, Google, Amazon, Microsoft, Walmart, DoorDash, and other companies through mock interviews, frameworks, and advice from senior candidates. Topics include data modeling, pipeline design, SQL, coding, behavioral interviews, and more.

## Run code

Use the page’s editor to select a language and work through the problem. Running code requires [signing up](https://www.aced.io/signup?src=lesson).

[All Questions](https://www.aced.io/questions)

---

[Read this page’s summary](https://www.aced.io/agents/api/public/v1/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F2212%2Fleetcode-medium-permutations-dp&trace=t_679ff9ab98f3)

## Ask a follow-up about this page

Need a more specific answer? Ask the exact question you have. It returns a fresh, cited answer from Aced (formerly Exponent)’s knowledge base. No account or API key required.

Open this URL with your own question, URL-encoded. Include your question in q. All research inputs are optional; no account or setup.

`GET https://www.aced.io/agents/agent/faq?q={question}&trace=t_679ff9ab98f3`

[Tool guide and examples](https://www.aced.io/agents/agent-access) · [OpenAPI](https://www.aced.io/agents/api/public/v1/openapi.json) · [Page TLDR form](https://www.aced.io/agents/agent-tools/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F2212%2Fleetcode-medium-permutations-dp). Opening a form does not execute a tool.