Skip to main content

45. Permutations

mediumAsked at Salesforce

Return all permutations of a distinct-integer array. Salesforce uses this as the cleanest backtracking template — perfect for testing 'used' tracking.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Salesforce backend onsite favorite for backtracking.
  • Blind (2025-12)Common warmup before harder permutation variants.

Problem

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

Constraints

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

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 = [1]
Output
[[1]]

Approaches

1. Backtracking with used set

Track which indices are used; recurse on unused; emit at full length.

Time
O(n! * n)
Space
O(n)
function permute(nums) {
  const result = [], used = new Array(nums.length).fill(false), path = [];
  function dfs() {
    if (path.length === nums.length) { result.push([...path]); return; }
    for (let i = 0; i < nums.length; i++) {
      if (used[i]) continue;
      used[i] = true; path.push(nums[i]);
      dfs();
      used[i] = false; path.pop();
    }
  }
  dfs();
  return result;
}

Tradeoff: Idiomatic backtracking. Salesforce's expected answer.

2. Swap-based in-place

For each position, swap with each later index, recurse, swap back. No extra space for tracking.

Time
O(n! * n)
Space
O(n) stack
function permute(nums) {
  const result = [];
  function dfs(start) {
    if (start === nums.length) { result.push([...nums]); return; }
    for (let i = start; i < nums.length; i++) {
      [nums[start], nums[i]] = [nums[i], nums[start]];
      dfs(start + 1);
      [nums[start], nums[i]] = [nums[i], nums[start]];
    }
  }
  dfs(0);
  return result;
}

Tradeoff: Avoids the used[] array but mutates input. Trade depends on whether mutation is acceptable.

Salesforce-specific tips

Salesforce uses backtracking templates everywhere — workflow-rule orderings, task dependency permutations. They grade on correct push/pop pairing on every iteration. Bonus signal: discuss how this generalizes to Permutations II (LC 47) — duplicates require sorted input + the 'skip if prev wasn't used' trick.

Common mistakes

  • Pushing path directly without slicing — same reference appears in result; mutation breaks output.
  • Forgetting to unset used[i] / pop path — corrupts state for the next iteration.
  • Off-by-one in the base case (path.length === nums.length is correct; length - 1 misses the last permutation).

Follow-up questions

An interviewer at Salesforce may pivot to one of these next:

  • Permutations II (LC 47) — with duplicates.
  • Next Permutation (LC 31).
  • Kth Permutation Sequence (LC 60).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why is the time complexity O(n! * n)?

There are n! permutations; copying each into the result takes O(n). Total work is O(n! * n).

When would I prefer the swap-based version?

When the input is mutable and you want to avoid O(n) bookkeeping. Most interview cases prefer the explicit used[] for clarity.

Practice these live with InterviewChamp.AI

Drill Permutations and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →