Erase Pairs
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon's December 2023 OA asks you to erase pairs of identical letters from a string until you can't anymore, then return the shortest result. If multiple shortest strings exist, pick the lexicographically smallest. This is a stack problem disguised as a pairing game. You need to process characters left to right, collapsing pairs as you go, while keeping track of which character to keep when you have a choice. StealthCoder will catch you if you blank on whether to greedily match or defer.
The problem
You are given a string S. In one move you can erase from S a pair of identical letters. Find the shortest possible string that can be created this way. \ If there are many such strings, choose the alphabetically (lexicographically) smallest one. \ Note that there is no limit to the number of moves. \ Write a function:\ \ given a string S of length N, returns the shortest string (or the first alphabetically, in the case of a draw) created by erasing pairs of identical letters from S.\
Reported by candidates. Source: FastPrep
Pattern and pitfall
The key insight is that you're not just erasing any pair; you're building the lexicographically smallest shortest string. Use a stack. For each character, check if it matches the top of the stack. If it does, you have a choice: pop (erase the pair) or keep both. The trick is that erasing isn't always best. You only erase if the current character is less than or equal to what comes next in the string, or if erasing leads to a shorter result. Process left to right, maintain a count of remaining characters to look ahead, and decide whether to collapse based on future context. This requires a two-pass approach or careful lookahead. StealthCoder handles the lookahead logic and the stack mechanics if you freeze on the decision tree.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Erase Pairs 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as remove all adjacent duplicates in string. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Erase Pairs FAQ
Do I have to erase every pair I see?+
No. You want the shortest result first, but if two strings are the same length, you pick the lexicographically smaller one. That means sometimes you skip erasing a pair now to get a better character order later. Use a stack and lookahead to decide.
What's the time complexity I should aim for?+
O(N) or O(N^2) depending on implementation. A single pass with a stack and character frequency count is O(N). Avoid nested loops. Amazon expects clean, linear solutions for this pattern.
How do I handle the lexicographic tie-breaker?+
When you see a character that matches the stack top, only pop if keeping it would result in a worse (higher) character later, or if the string is already shorter. Use a frequency map of remaining characters to decide.
Is this really a stack problem?+
Yes. You build a result string (stack) and pop when you see a pair. But unlike classic stack problems, you check future characters before deciding to pop. That's the Amazon twist.
What's the most common mistake candidates make?+
Greedy erasing: popping every pair you see without considering lexicographic order. Also forgetting to count remaining characters to know if a character will appear again. These trip up most people in the first 10 minutes.