Unique String Permutations
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure that carries this one is a frequency map of characters. Amazon candidates reported Unique String Permutations in September 2026, and it looks like a classic permutations problem until duplicates show up. Strings like "aab" and "xxyy" are there to punish anyone who generates everything and hopes for the best. The output has to be distinct and in ascending lexicographic order, with length capped at 8. If you blank when the OA clock is running, StealthCoder is the safety net that reads the problem on screen and hands you a working solution without the proctor seeing it.
The problem
Given a lowercase string s, return every distinct permutation of its characters exactly once. Return the permutations in ascending lexicographic order. Equal characters from different input positions must not create duplicate strings. Function uniquePermutations(s: String) → String[] Examples Example 1 s = "aab" return = ["aab","aba","baa"] There are three distinct arrangements after duplicate swaps of the two a characters are removed. Example 2 s = "xxyy" return = ["xxyy","xyxy","xyyx","yxxy","yxyx","yyxx"] The four positions have six unique arrangements because each character appears twice. Example 3 s = "z" return = ["z"] A one-character string has one permutation. Constraints 1 <= s.length <= 8. s contains only lowercase English letters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Count the characters, then backtrack over the distinct letters instead of over positions. Use a 26-slot count array or a sorted map. At each step, loop through letters in ascending order, skip any with a count of zero, decrement the count, append the letter, recurse, then undo both. Because you iterate letters in sorted order, the results come out lexicographically sorted with no extra sort. Duplicates can't appear because you pick a letter value once per level, not a position. The common pitfall is generating all n! permutations into a set and sorting, which works at length 8 but looks sloppy and can hit time or memory limits on bigger tests. Another trap is the sort-and-skip-previous trick done with a wrong used-array condition. With the count approach, that bug is gone. If the live OA freezes you, StealthCoder is the hedge that gives you this exact structure.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Unique String Permutations cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as permutations ii. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Unique String Permutations FAQ
What's the trick to Unique String Permutations?+
Backtrack on a character frequency count instead of on indices. At each level, try each letter that still has a count above zero, in alphabetical order. That prevents duplicates by construction and gives sorted output for free.
How hard is this really for an Amazon OA?+
Medium. The base permutation idea is easy, but duplicates and sorted output trip people up. With length capped at 8, brute force plus a set passes, so the real test is whether you write the clean version quickly.
Can I just generate all permutations and dedupe with a set?+
Yes, at length 8 that's at most 40320 strings, so it passes. Sort the set at the end. It's slower and less elegant than the count-based backtracking, but it's a valid fallback if you're short on time.
How do I get lexicographic order without sorting at the end?+
Iterate the letters a to z (or a sorted list of the distinct characters) at every recursion level. Since earlier letters are explored first, the finished strings are emitted in ascending order automatically.
How do I prepare for this in 48 hours?+
Write the count-based backtracking from scratch twice, using "aab" and "xxyy" as tests. Then do the sort-and-skip-duplicate variant so you know both. Focus on the undo step, since forgetting to restore the count is the usual bug.