String Permutations in Custom Character Order
Reported by candidates from Wex's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Wex OA, reported in September 2026, is generating permutations first and sorting with default string comparison. ASCII puts uppercase before lowercase, and the problem wants the opposite. This is a distinct-permutations question with a custom ordering, capped at length 7, so the input is tiny and the trap is all in the details. Duplicates, the empty string, and character order are where points get lost. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and hands you a working solution in real time. Know the shape before you sit down.
The problem
Given a string s containing digits, lowercase English letters, and uppercase English letters, return every distinct permutation of its characters in the custom lexicographic order defined below. Digits come first, in order 0 through 9. Lowercase letters come next, in order a through z. Uppercase letters come last, in order A through Z. Compare two permutations at their first different character using this order. Each permutation must use every character of s exactly as many times as it appears in s. For this exercise, assume identical permutations caused by repeated characters appear only once. The empty string has exactly one permutation: the empty string. Function sortedPermutations(s: String) → String[] Examples Example 1 s = "0aA" return = ["0aA","0Aa","a0A","aA0","A0a","Aa0"] Permutations beginning with 0 come first, followed by those beginning with a, then A. Within each group, the same custom order applies to the remaining characters. Example 2 s = "b1b" return = ["1bb","b1b","bb1"] The two identical b characters produce only three distinct permutations. The digit 1 comes before b. Example 3 s = "" return = [""] Using every character of an empty string produces the empty string once. Constraints 0 <= s.length <= 7. Each character is an English letter or a digit.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to sort the characters once using the custom order (digits, then lowercase, then uppercase), then run backtracking with a used array. At each depth, loop through the sorted characters in order. Skip a character if it's used, or if it equals the previous character and that previous copy is unused. That skip rule kills duplicates without a set. Because you pick in sorted order, the output is already in the right lexicographic order, so no final sort is needed. The pitfall is calling plain sort() on the input, which puts uppercase before lowercase and breaks Example 1. Also handle the empty string by returning a list with one empty string. With length at most 7, the worst case is 5040 results, so performance isn't the issue. StealthCoder is your hedge on the live OA if the dedupe condition slips your mind under pressure.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill String Permutations in Custom Character Order 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
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 Wex's OA.
Wex reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
String Permutations in Custom Character Order FAQ
What's the trick in this Wex permutations problem?+
Sort the characters with a custom key first, then backtrack picking characters in that sorted order. The output comes out already ordered. Dedupe by skipping a character when it matches the previous one and the previous copy isn't used in the current path.
How do I sort digits, lowercase, then uppercase?+
Use a sort key instead of default comparison. Map digits to 0, lowercase to 1, uppercase to 2, then break ties by the character itself. Default ASCII order puts uppercase before lowercase, which fails Example 1 immediately.
Do I need a set to remove duplicate permutations?+
No. A set works at length 7, but the cleaner approach is the sorted-skip rule in backtracking. If s[i] equals s[i-1] and s[i-1] is unused, skip it. This generates each distinct permutation exactly once and keeps the order intact.
What edge cases should I test before submitting?+
Test the empty string, which must return a list containing one empty string. Test all identical characters like aaa, which gives one result. Test a mixed input like 0aA to confirm digits, lowercase, uppercase ordering matches the examples.
How do I prepare for this in 48 hours?+
Write the distinct-permutations backtracking template from memory twice. Then swap in the custom comparator and run Examples 1 to 3 by hand. That's enough. The constraint of length 7 means brute-force generation is fine, so don't over-optimize.