Generate Strings of Length at Least Three
Reported by candidates from Morgan Stanley's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Morgan Stanley OA reported in September 2026 looks like a simple permutation problem, and then one detail trips people up: you need every arrangement of length 3 up to n, not just full-length ones. It's a backtracking question on a string of at most 7 distinct letters. If you've got an invite in your inbox, this is one to walk through before the clock starts. And if you blank mid-assessment, StealthCoder runs invisibly as a safety net, so one lost minute doesn't sink the attempt.
The problem
Given a string of distinct lowercase characters, generate every string formed without reusing a character whose length is at least three and at most the input length. Return the strings in lexicographic order. Function generateStrings(characters: String) → String[] Examples Example 1 characters = "abc" return = ["abc","acb","bac","bca","cab","cba"] Case 1 exercises the documented deterministic contract. Example 2 characters = "abcd" return = ["abc","abcd","abd","abdc","acb","acbd","acd","acdb","adb","adbc","adc","adcb","bac","bacd","bad","badc","bca","bcad","bcd","bcda","bda","bdac","bdc","bdca","cab","cabd","cad","cadb","cba","cbad","cbd","cbda","cda","cdab","cdb","cdba","dab","dabc","dac","dacb","dba","dbac","dbc","dbca","dca","dcab","dcb","dcba"] Case 2 exercises the documented deterministic contract. Example 3 characters = "cba" return = ["abc","acb","bac","bca","cab","cba"] Case 3 exercises the documented deterministic contract. Constraints 3 <= characters.length <= 7. All characters are distinct lowercase English letters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The pattern is backtracking. Sort the input first. Then build a path one character at a time, using a used array so nothing repeats. Every time the path length hits 3 or more, record it. Keep recursing until the path equals the input length. The trap is the edge case: if you only record at full length, you miss all the shorter strings. If you record at every depth, don't record lengths 1 and 2. Sorting up front is what gives you lexicographic order for free, since you always try characters in ascending order. Example 3 uses "cba" and expects sorted output, which is the hint. Skip a final sort and you pay for it on unsorted input. With n at most 7 the output is small, around 8000 strings, so performance isn't the issue. Correctness on the length rules is. If you freeze live, StealthCoder is the hedge that reads the problem and hands you the recursion.
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 Generate Strings of Length at Least Three 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
You've seen the question.
Make sure you actually pass Morgan Stanley's OA.
Morgan Stanley 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.
Generate Strings of Length at Least Three FAQ
What's the trick in this Morgan Stanley OA question?+
Record the path at every depth from 3 up to n, not only at full length. Backtrack with a used array and sort the characters first. Most wrong answers either miss the shorter strings or include lengths 1 and 2.
How do I get lexicographic order without sorting the output?+
Sort the input characters once at the start. Then your loop always tries characters in ascending order, and within each prefix, shorter strings appear before longer extensions. That matches the expected order in Example 2, where abc comes before abcd.
How hard is this really?+
Easy to medium. It's a standard permutation backtracking problem with one twist on length. With n at most 7, there's no optimization needed. If you've written permutations before, you can finish it in about 15 minutes.
What's the time complexity?+
It's roughly the sum of n!/(n-k)! for k from 3 to n, times the string length to build each result. For n of 7 that's a few thousand strings at most, so a plain recursive solution is fine.
How do I prepare in 48 hours?+
Write permutations with a used array from scratch twice. Then change it to record at every depth with a minimum length. Test on "cba" and "abcd" to check the ordering and the count of outputs. That covers this problem and its close variants.