Coding · must-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix
Permutations
mediumbacktracking
Problem
Given an array nums of distinct integers, return every possible ordering of its elements. The orderings may be returned in any sequence, but each must appear exactly once.
- Up to 8 elements.
- Each value is between -20 and 20.
- All values are distinct.
Examples
nums = [4, 5, 6]→[[4,5,6], [4,6,5], [5,4,6], [5,6,4], [6,4,5], [6,5,4]]in any order;3! = 6results.nums = [0, 8]→[[0,8], [8,0]].nums = [7]→[[7]].
Hints
- Fix the element that goes first. How many ways are there to arrange the rest, and does that sub-problem look like the original?
- Keep track of which elements are already placed so you never reuse one within the same ordering.
- Backtracking with a
usedarray (or in-place swapping); record the path when its length reachesn.
Last reviewed 8 September 2026
Locked
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Secure payment by Stripe · instant access · refund questions: [email protected]
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.