Lexicographically Minimum String After Removing Stars
A medium-tier problem at 51% community acceptance, tagged with Hash Table, String, Stack. Reported in interviews at Flexera and 0 others.
You're asked to remove characters from a string to minimize its lexicographic order, and the twist is that removals happen via 'stars' that erase the character to their left. The problem looks like a stack problem until you realize greedy won't work on the first pass. Flexera has asked this one. The acceptance rate sits around 51%, which means half the people who attempt it get stuck on the strategy. Most candidates either try a naive greedy approach or miss that you need to revisit earlier decisions. If this problem hits your live OA and you freeze on the pattern, StealthCoder solves it in seconds, invisible to the proctor.
Companies that ask "Lexicographically Minimum String After Removing Stars"
Lexicographically Minimum String After Removing Stars is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too.
Get StealthCoderThe core insight is that you can't just greedily pick the smallest character you see. You need a way to reconsider earlier choices once you know what characters are coming later. A stack works, but the trick is knowing when to pop an element that you've already committed to. The optimal approach uses a greedy strategy with lookahead: for each star, pop the previous character only if a smaller character exists later in the string. A heap or priority queue helps you efficiently track what's coming next. Many candidates start with a simple stack and get wrong answers because they don't account for the ability to swap out a larger character for a smaller one. The key is preprocessing to know the minimum character from each position onward, then using a stack with that information to decide pops. StealthCoder bridges the gap between 'stack sounds right' and 'but my logic is broken.'
Pattern tags
You know the problem.
Make sure you actually pass it.
Lexicographically Minimum String After Removing Stars recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Lexicographically Minimum String After Removing Stars interview FAQ
Is this actually a stack problem or greedy problem?+
It's both. You use a stack to build the result, but the decision to pop is greedy and requires lookahead. You need to precompute the minimum character from each position forward, then use that information while processing the string left to right with a stack.
Why does a naive greedy approach fail?+
A naive greedy approach tries to always keep the smallest character, but you can't undo a pop once you've made it without redesigning the whole stack. The trick is deciding pop eligibility upfront by knowing what smaller characters exist later, so you only pop when you're guaranteed to improve the result.
Do I need a heap or priority queue for this?+
A heap is one valid approach for tracking the minimum character from each position onward, but it's not strictly necessary. You can precompute the answer with a simple right-to-left scan before processing the stack. Both work; the heap approach is more intuitive for some.
How do I know when to pop on a star?+
Pop the top of the stack only if a character smaller than it exists somewhere after the current position. Use a precomputed 'minimum suffix' array to check this in O(1). Without this check, you'll remove characters you should keep.
What's the time complexity and is it acceptable for the input size?+
The optimal solution is O(n) with a single stack pass and a precomputation pass. Even with a heap assist, you stay O(n log n) at worst, which is solid for strings up to 10^5 characters. The acceptance rate around 51% suggests many people get stuck on implementation or correctness before hitting time-limit issues.
Want the actual problem statement? View "Lexicographically Minimum String After Removing Stars" on LeetCode →