Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Medium Backtracking High frequency Solution Available Practice Mode

Permutations LeetCode Solution

Tags: #Array #Backtracking

Approach Summary

Swap elements to place each number at index 0 recursively, or use a used[] array and add unused elements at each step.

Practice Mode

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Backtracking Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Backtracking pattern.

Framework: Backtracking • Interview Prep: Tested at FAANG • Explore all Backtracking problems & invariants →

How to Recognize This Pattern

  • Generate all permutations
  • No duplicate numbers

Complexity Analysis

Time Complexity

O(n × n!)

Space Complexity

O(n)

Tags

Array Backtracking

Frequently Asked Questions

What is the optimal time and space complexity for Permutations?
The optimal solution for Permutations achieves O(n × n!) runtime complexity and requires O(n) auxiliary space. There are n! permutations, each costing O(n) to build and copy — O(n × n!) time. The recursion depth is n and the path holds n elements — O(n) space.
How do I identify that Permutations requires the Backtracking pattern?
In technical interviews, look for key problem signals: Generate all permutations; No duplicate numbers. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Backtracking strategy.
What critical edge cases must be handled in Permutations?
Essential edge cases to verify include: Empty array — return [[]]; Single element — one permutation; Repeated values — not in this problem; Permutations II adds a sort-and-skip guard. Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask Permutations?
Permutations is a Medium-difficulty question tagged with high recurrence. It is actively asked in technical screening and onsite interview loops at LinkedIn, Microsoft, Amazon.
Buy me a coffee
Daily Practice
0/0