Get Final String
Reported by candidates from IBM's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
IBM's December OA is testing string manipulation under time pressure, and this one's a pattern-matching game that trips up candidates who overthink it. You've got a string and a set of operations, and the goal is to reduce it to a final form. StealthCoder reads the problem and feeds you the reduction pattern in real time, so if you blank on the order of operations, you've got a safety net. The trick isn't complexity. It's understanding what actually reduces and when to stop.
Pattern and pitfall
Get Final String is a string reduction problem where you apply substitution rules repeatedly until no more reductions are possible. The key is understanding that each rule replacement can trigger new matches further down the string, so you often need to re-scan after each operation. Many candidates code a single pass, miss cascading reductions, and return the wrong result. The pattern is iterative substitution with backtracking through the modified string. If you code it naively, you'll TLE or return incorrect output. The trick is to build the string character by character, checking if the last few characters now match a rule, and popping if they do. StealthCoder shows you the exact rule set and how many iterations you need, which removes guesswork during the live OA.
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 Get Final 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 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 IBM's OA.
IBM 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.
Get Final String FAQ
How many reduction rules are there, and do I have to hardcode them?+
The problem gives you the rules as input. Don't hardcode. Parse them and store as substitution pairs. IBM's variant usually includes 2-5 rules. Read the rules from input, apply them in order, and re-check the string until no more reductions occur.
What's the most common mistake on this one?+
Applying rules once and returning. You need to re-scan the entire string after each replacement because new patterns can emerge. A single pass will fail test cases. Use a loop that keeps applying rules until the string stops changing.
Is this a greedy problem or do I need DP?+
It's iterative simulation with a greedy reduction strategy. No DP needed. The trick is using a stack-like approach or careful re-indexing. Build the result character by character, checking if adding the new char creates a match with the last few characters.
How do I avoid TLE on long strings?+
Don't rebuild the entire string after each rule. Use a deque or list and pop/append as you match rules. Check for matches only when you add a character or perform a replacement, not the whole string every iteration.
Does the order of rules matter?+
Yes. Apply rules in the order given. If rule A matches before rule B, you apply A first. The problem statement should specify if you apply only the first matching rule or all rules in sequence. Read carefully.