Reported October 2026
Oraclestack

Remove Adjacent Duplicates in String II

Reported by candidates from Oracle's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Oracle OA. Under 2s to a working solution.
Founder's read

With s.length up to 100000, rescanning the string after every removal will time out, and that's the trap in this Oracle OA reported in October 2026. The problem is Remove Adjacent Duplicates in String II: strip any run of exactly k equal neighbors, let the leftovers collapse together, and repeat until nothing's left to remove. It's a string problem with a stack hiding inside it. Once you see that, it's a ten-minute solve. If you blank on the live assessment, StealthCoder runs invisibly as a safety net and hands you the approach in real time.

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

The trick is a stack of (character, count) pairs. Walk the string once. If the current character matches the top of the stack, increment that count. Otherwise push a new pair with count 1. When a count hits k, pop the pair. Removals that make new neighbors touch are handled automatically, because the pair beneath the popped one is now the top. At the end, rebuild the string by repeating each character by its count. That's O(n) time and O(n) space. The common pitfall is the brute-force loop: scan, delete, rescan. That's O(n^2) or worse and dies at 100000 characters. Another slip is forgetting to reset the count after a pop, or comparing against the wrong neighbor. Trace Example 1 by hand before you submit. If your head locks up during the OA, StealthCoder is the hedge that reads the problem and gives you the stack solution.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as remove all adjacent duplicates in string ii. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Oracle's OA.

Oracle reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Remove Adjacent Duplicates in String II FAQ

What's the trick for Remove Adjacent Duplicates II?+

Use a stack of (character, count) pairs. Matching the top increments its count, a mismatch pushes a new pair, and hitting k pops it. One pass handles chain reactions, since the pair below becomes the new top. Rebuild the string from the surviving pairs at the end.

How hard is this problem really?+

Medium. The logic is short once you know the stack idea. The difficulty is realizing that cascading removals don't need rescanning. If you've done the k=2 version, this is the same thing with a counter added to each stack entry.

Why does brute force fail here?+

The string can be 100000 characters long. Repeatedly scanning for a group of k, deleting it, and rescanning can approach quadratic time. That's too slow at this size. The stack solution touches each character once, so it stays linear.

Can I solve it with two pointers instead of a stack?+

Yes. Use the string as an in-place array with a write pointer and a parallel count array. When a count reaches k, move the write pointer back by k. It's the same logic as the stack, with slightly less memory overhead. Either approach is accepted.

How do I prepare for this in 48 hours?+

Code the stack version from scratch twice. Test it on the three examples, plus a case where the whole string vanishes and one with a run longer than k. Check that your count resets properly after a pop. That covers nearly every bug people hit.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Oracle.

OA at Oracle?
Invisible during screen share
Get it