Get Alphabetically Smallest String
Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Microsoft's June 2024 OA includes a string manipulation problem where you're asked to produce the alphabetically smallest possible string from a given input. The catch is figuring out what operations are allowed and whether you're rearranging, removing, or swapping characters. This is a pattern candidates often blank on because the problem statement can be deceptively vague. StealthCoder reads the exact constraints from the screen and feeds you the right approach in real time, so you don't waste 15 minutes guessing at the rules.
Pattern and pitfall
The trick here is always in the constraint details. You'll either be allowed to rearrange all characters freely (in which case: sort), remove characters selectively (greedy approach: skip anything larger than what comes next), or perform a limited number of swaps. The common pitfall is coding the obvious solution first, then realizing mid-way through that you misread the operation type. String problems at Microsoft tend to reward greedy thinking: at each position, place the smallest character you're allowed to place there without violating future constraints. StealthCoder sits beside you during the assessment and confirms the exact rule set, so if you misread the problem initially, you catch it before submitting wrong code.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Get Alphabetically Smallest String 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 would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as remove duplicate letters. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Microsoft's OA.
Microsoft reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Get Alphabetically Smallest String FAQ
Is this just sorting the string?+
Not necessarily. If the problem allows full rearrangement, yes. But most versions have a twist: you can only remove characters, or swap adjacent pairs, or perform a limited number of operations. Read the constraint section carefully. That's where the algorithm branches.
What if I can only swap adjacent characters?+
Then you're doing a constrained bubble sort. Build the smallest result greedily: for each position, find the smallest character within your swap budget, then bubble it left. This scales to O(n squared) in worst case but is the intended approach.
Do I need to handle lexicographic ordering edge cases?+
Yes. Two strings of different lengths are compared character by character, stopping at the first difference. A shorter string is 'smaller' if it's a prefix of a longer one. Make sure your solution doesn't accidentally skip valid shorter results.
How do I avoid timeouts on large inputs?+
Don't re-sort or re-scan the entire string repeatedly. Use a single forward pass with a greedy choice at each step, or precompute suffix minimums if you need lookahead. O(n) or O(n log n) is the target.
What if the problem lets me remove characters?+
Greedy works: iterate left to right, skip a character if a smaller one appears later. This gives the lexicographically smallest subsequence. Think of it as 'commit to keeping this character only if nothing better is coming.'