Substring Removal
Reported by candidates from JP Morgan's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The JP Morgan Substring Removal question, reported in October 2026, looks like a string puzzle but it's really a stack problem. You delete "AB" or "BB" from a string of A's and B's and want the shortest leftover. With a string up to 2 * 10^5, brute-force deleting and rescanning dies fast. If you're taking this OA in a day or two, learn the stack idea now, because it's about ten lines of code. StealthCoder sits invisibly on your screen as a safety net if your mind goes blank mid-assessment, but you shouldn't need it once you see the trick.
The problem
Given a string, seq, that consists of the characters 'A' and 'B' only, in one move, delete either an "AB" or a "BB" substring and concatenate the remaining substrings. Find the minimum possible length of the remaining string after performing any number of moves. Note: A substring is a contiguous subsequence of a string. Function getMinLength(seq: String) → int Complete the function getMinLength in the editor below. getMinLength has the following parameter(s): string seq: the string Returns int: the minimum possible length of the remaining string Examples Example 1 seq = "BABBA" return = 1 Using 0-based indexing, the following moves are optimal. Delete the substring "AB" starting at index 1. "BABBA" → "BBA" Delete the substring "BB" starting at index 0. "BBA" → "A" There are no more moves, so the minimum possible length of the remaining string is 1. Constraints 1 ≤ |seq| ≤ 2 * 10^5 The string only contains characters 'A' and 'B'.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The hinted sliding window doesn't fit well. The real tool is a stack, or even a simple counter. Scan left to right. Push each character. If the top of the stack is 'A' or 'B' and the incoming char is 'B', you can delete the pair, since both AB and BB end in B. So any incoming B pops the top if the stack is nonempty. An incoming A just gets pushed. Return the stack size. The pitfall is simulating deletions with string replace in a loop, which is quadratic and times out. Another trap is greedy confusion about which pair to remove first. Order doesn't matter here, because every B cancels one character before it. You can even skip the stack and track a count. StealthCoder is your hedge if the live OA wipes your memory, but the logic is short enough to memorize tonight.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Substring Removal 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
You've seen the question.
Make sure you actually pass JP Morgan's OA.
JP Morgan 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.
Substring Removal FAQ
What's the trick in the JP Morgan Substring Removal problem?+
Every deletable pair ends in B. So when you hit a B and there's any character before it still alive, that B removes it. Use a stack: push A's, and on B pop if the stack isn't empty, otherwise push B. The answer is the final stack size.
Is this really a sliding window problem?+
No. The hint says sliding window, but nothing here involves a moving range or window constraint. Deletions collapse the string, and that's what stacks handle. Treat it as a stack or greedy counting problem and you'll get a clean O(n) solution.
How hard is Substring Removal really?+
Easy to medium. The idea is short once you spot it, but people burn time on string replace loops. With |seq| up to 2 * 10^5, you need linear time. If you've done bracket-matching or adjacent-duplicate removal problems, this will feel familiar.
Can I solve it without a stack?+
Yes. Keep a length counter. For each char, if it's B and the counter is above zero, decrement. Otherwise increment. This works because a B always cancels one previous character, whether A or B. It's O(n) time and O(1) space, and it matches the stack answer.
How do I prepare for this in 48 hours?+
Code the stack version from memory twice, then the counter version. Test on "BABBA" (answer 1), all A's, all B's, and a single character. Then do two or three adjacent-removal stack problems. That covers this pattern and its close variants well enough for the OA.