Remove Adjacent Duplicates in String II
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks most first attempts at this Bloomberg question, reported in February 2021, is rescanning the string after every removal. It looks fine on the samples and dies on a 100000-character input. This is Remove Adjacent Duplicates in String II, and the real pattern is a stack of character and run-count pairs. If you've got an OA invite and 48 hours, learn that one idea cold. StealthCoder sits invisibly on your screen as a safety net during the live assessment if your mind goes blank, but the stack approach is short enough to own yourself.
The problem
Given a lowercase string s and an integer k, repeatedly remove any group of exactly k adjacent equal characters. Concatenate the remaining parts after each removal. Return the unique final string after no removable group remains. Function removeDuplicates(s: String, k: int) → String Examples Example 1 s = "deeedbbcccbdaa" k = 3 return = "aa" Removing eee and ccc makes the three b characters adjacent. Removing them leaves aa. Example 2 s = "pbbcggttciiippooaais" k = 2 return = "ps" Each adjacent pair is removed as it forms, including pairs created by earlier removals. Example 3 s = "abcd" k = 2 return = "abcd" No two adjacent characters are equal. Constraints 1 <= s.length <= 100000. s contains only lowercase English letters. 2 <= k <= s.length.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Walk the string once and keep a stack of pairs: a character and how many times it's repeated in a row. For each new character, if it matches the top, increment the count. If it doesn't, push it with a count of 1. When a count hits k, pop that entry. Chain reactions happen for free, because after a pop the next character compares against whatever is now on top. That's how eee and ccc vanish and the b's merge in Example 1. The pitfall is the brute-force approach: repeated string slicing and rescanning gives O(n^2) at n = 100000. Another slip is merging counts incorrectly after a pop, or forgetting to rebuild the answer by repeating each character count times. This runs in O(n) time and O(n) space. If you freeze mid-OA, StealthCoder can hand you this stack solution in real time without the proctor seeing it.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Remove Adjacent Duplicates in String II 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as remove all adjacent duplicates in string ii. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Remove Adjacent Duplicates in String II FAQ
How hard is Remove Adjacent Duplicates II really?+
It's a medium. The logic is short once you see the stack of (char, count) pairs. Most people lose time on a brute-force rescan that times out at 100000 characters. If you know the stack idea, you can code it in about ten minutes.
What's the trick to solving it fast?+
Store a count with each character on the stack. Increment when the new character matches the top, pop when the count reaches k. Because you compare against the new top after a pop, cascading removals like the b's in Example 1 resolve automatically in a single pass.
Why does the brute-force approach fail?+
Repeatedly searching for k equal neighbors and rebuilding the string costs O(n) per removal, and removals can number in the thousands. With s up to 100000 characters that becomes O(n^2) and times out. A single-pass stack keeps it linear.
Is this pattern still asked by Bloomberg?+
This one was reported in February 2021, and stack-based string reduction is a staple pattern for string OAs generally. Expect variants: different k, different removal rules, or the k=2 version. Learn the stack-with-counts idea and you cover all of them.
How do I prepare in 48 hours?+
Write this solution from scratch twice, then trace Example 1 and Example 2 by hand. Check edge cases: k equal to the string length, no removals at all, and a full collapse to an empty string. Then write the output step by repeating each character by its count.