← Questions
Permutations of a list with duplicates
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Meta's AI-enabled coding round, last asked Sep 2025. It tests backtracking and recursion.
Given a list of integers that may contain repeated values, return every distinct ordering of it, with no ordering repeated in the output.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
permute_unique(nums: list[int]) -> list[list[int]]— every distinct permutation, in any order.
Example
permute_unique([1, 1, 2]) -> [[1, 1, 2], [1, 2, 1], [2, 1, 1]]
permute_unique([1, 1, 1]) -> [[1, 1, 1]]
permute_unique([1, 1, 2, 2]) has 6 results # 4! / (2! * 2!)
Constraints
-
1 <= len(nums) <= 8,-10 <= nums[i] <= 10. - A value may appear more than twice.
Clarifying questions
- Does the output need to be in a particular order?
- Can I sort the input or change it in place?
- Is it fine to generate duplicates and remove them with a set? (Usually not: show the version that never makes them.)
What a strong answer covers
- Backtracking over a frequency map: at each position, try each distinct value that has copies left. No duplicates are ever generated.
- Or sort first and skip
nums[i]when it equalsnums[i-1]andnums[i-1]is not in use at this depth, with an explanation of why that rule removes exactly the repeats. - A correctness argument: each distinct sequence corresponds to exactly one path of choices.
- Complexity: the number of results is
n! / (c1! * c2! * ...), each costing O(n) to copy. O(n) extra space besides the output. - Tests: all values equal, all values distinct, values that appear three or more times.
Follow-ups
- Generate the next permutation in lexicographic order, in place.
- Return only the
k-th distinct permutation in sorted order without generating the others. - Distinct combinations of size
rfrom a list with duplicates.
Where it's asked
- Meta · AI-enabled coding · Last asked Sep 202560 min · Several progressive tasks on a starter codebase in CoderPad with an AI assistant panel; usually two rounds · The first task usually without AI, later tasks with it
What it tests
backtracking · recursion
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
