Given an array of distinct integers, explain how you would generate all possible permutations.
For example, given [1, 2, 3], the permutations are: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1].
Describe your approach, explain how you avoid using the same element twice in a single permutation, and analyze the time and space complexity.
How to approach it
- Hint 1
At each position in the permutation, you choose from the elements not yet used. How do you track which elements are available?
- Hint 2
Think of building the permutation one position at a time -- at position k you have (n - k) choices remaining.
- Hint 3
A boolean visited array or swapping elements in place are two common ways to track usage.
Ready to answer it out loud?
Record your answer in 1:30 and Preptile scores it 1–10 with specifics — what landed, what you skipped, and what to say next time.
Practising needs an invite code. Join the waitlist and we’ll send you one.