Reported March 2020
Bloombergbacktracking

Generate All String Permutations

Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Bloomberg OA. Under 2s to a working solution.
Founder's read

The Bloomberg OA reported in March 2020 asks you to generate all permutations of a string, and the trap is the output order. Most people write the backtracking in five minutes, then fail hidden tests because the results aren't lexicographic when the input is something like "ba". Characters are distinct and the length caps at 8, so the work is small. The only real question is whether you sort first. If you blank on the ordering or the recursion in the live assessment, StealthCoder runs invisibly on your desktop and hands you a working solution in real time. Know the trick before you need it.

The problem

Given a string s whose characters are distinct lowercase English letters, return every permutation of its characters.
Return the permutations in lexicographic order.

Function
generatePermutations(s: String) → String[]

Examples
Example 1
s = "abc"
return = ["abc","acb","bac","bca","cab","cba"]
All six arrangements of the three distinct characters are returned in lexicographic order.
Example 2
s = "ba"
return = ["ab","ba"]
The input order does not determine the output order; the two permutations are sorted lexicographically.
Example 3
s = "x"
return = ["x"]
A one-character string has exactly one permutation.

Constraints
1 <= s.length <= 8
s contains distinct lowercase English letters.

Reported by candidates. Source: FastPrep

Pattern and pitfall

This is backtracking with a used-array. Sort the characters first, then build each permutation one position at a time, always trying unused characters in sorted order. Because you pick the smallest available character first at every level, the output comes out lexicographic with no extra sort at the end. The common pitfall is permuting the raw input with swap-based recursion. That produces valid permutations in the wrong order, and Example 2 with "ba" exposes it immediately. Another slip is forgetting to undo the used flag or pop the character after recursing. With length at most 8, you have at most 40320 results, so time isn't a concern. Mind the one-character case, which should return a single-element list. If the recursion stack or ordering logic falls apart mid-assessment, StealthCoder is the hedge that keeps you moving without the proctor seeing anything.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Generate All 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. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as permutations. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Bloomberg's OA.

Bloomberg reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Generate All String Permutations FAQ

What's the trick in the Bloomberg generate permutations problem?+

Sort the string first, then backtrack choosing unused characters in ascending order. That guarantees lexicographic output without a final sort. The trap is swap-based permutation, which gives correct sets but wrong order, especially on inputs like "ba" where the input isn't already sorted.

How hard is this problem really?+

Easy to medium. It's a standard backtracking template. With length capped at 8 and distinct letters, there's no duplicate handling and no performance pressure. Most failures come from output ordering or forgetting to reset state after the recursive call.

Do I need to sort the output at the end?+

Not if you sort the input characters first and iterate them in order at each recursion level. If you'd rather not think about it, sorting the final list is also fine at this size. Either approach passes, but pick one deliberately.

What's the time complexity?+

O(n * n!) because there are n! permutations and each takes O(n) to build and copy. With n at most 8, that's at most 40320 strings, which is trivial. Add O(n log n) for the initial sort, which doesn't matter.

How do I prepare in 48 hours for this OA?+

Write the used-array backtracking template from memory twice, once with a string builder and once with a list. Test it on "abc", "ba", and "x". Then do one subsets or combinations problem so the pattern feels automatic. Don't spend time on anything fancier.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Bloomberg.

OA at Bloomberg?
Invisible during screen share
Get it