Stack Batch Removal
Reported by candidates from IMC's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Two hundred thousand operations is the number that kills the obvious answer. The IMC Stack Batch Removal question, reported in August 2026, looks like a warm-up stack problem until remove_lower and remove_upper show up. Scanning the whole stack on every removal is O(n^2) and it won't survive the input size. You need a stack plus something that finds the smallest and largest values fast. If your OA is a day or two out, learn the shape before you sit down. StealthCoder runs invisibly on your screen as a safety net if you blank mid-assessment, but this idea is short enough to own yourself.
The problem
Implement a stack that accepts the following commands and performs the operations described: push value: Push integer value onto the top of the stack. pop: Pop the top element from the stack. remove_lower value: Remove all current elements in the stack less than value. remove_upper value: Remove all current elements in the stack more than value. After each operation, output the current top element of the stack. If no such element exists, output "EMPTY". The input array operations contains the command lines in order. Return one output string for each operation, in the same order as the lines the original program prints. Function stackBatchRemoval(operations: String[]) → String[] Examples Example 1 operations = ["push 3", "push 2", "push 4", "remove_lower 3", "remove_upper 3", "push 2", "pop", "pop"] return = ["3", "2", "4", "4", "3", "2", "3", "EMPTY"] Initially, the stack is empty. After push 3, the stack contains [3]. After push 2, the stack contains [3, 2]. After push 4, the stack contains [3, 2, 4]. After remove_lower 3, the stack contains [3, 4]. After remove_upper 3, the stack contains [3]. After push 2, the stack contains [3, 2]. After pop, the stack contains [3]. After pop, the stack is empty. Constraints 1 <= n <= 2 * 10^5, the total number of lines or operations. -10^9 <= value <= 10^9 It is guaranteed that pop will not be called on an empty stack.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Keep a plain stack of entry ids, a min-heap and a max-heap of (value, id) pairs, and a removed flag per id. Push adds to all three. remove_lower v pops the min-heap while its top is below v, flagging each id as removed. remove_upper does the same with the max-heap. Pop first discards flagged ids on top of the stack, then pops the real top and flags it too. After every operation, clean the stack top the same way and print it, or EMPTY. Each element is flagged once and leaves each structure once, so the total is O(n log n). The classic pitfall is forgetting that pop must flag the id, so a heap later removes a ghost. The other is the inequality: equal values stay. If lazy deletion slips your mind live, StealthCoder is the hedge that surfaces this structure while the clock runs.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Stack Batch 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. 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
You've seen the question.
Make sure you actually pass IMC's OA.
IMC 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.
Stack Batch Removal FAQ
How hard is IMC Stack Batch Removal really?+
Medium. The stack part is trivial. The difficulty is that removals hit the middle of the stack, and with up to 2 * 10^5 operations a full scan each time is too slow. Once you see lazy deletion with heaps, it's maybe 30 lines of code.
What's the trick to beat the time limit on this one?+
Amortize. Every element can only be removed once, so use a min-heap and max-heap to find removal candidates in log time, and mark them dead instead of deleting from the stack. Skip dead entries only when they reach the top.
Why can't I just use a single stack and loop through it?+
Because remove_lower and remove_upper can fire on every operation. With a large stack, each scan costs O(n), so the total goes to O(n^2). At 2 * 10^5 operations that's tens of billions of steps in the worst case.
What edge cases should I test before submitting?+
Test values equal to the threshold, since only strictly lower or higher get removed. Test a removal that empties the stack, then a push after it. Test duplicates, negative values, and a pop right after a removal exposed a dead entry underneath.
How do I prepare for this in 48 hours?+
Write the lazy-deletion version once from scratch. Use ids, two heaps, and a removed array. Trace Example 1 by hand, then code a brute force and compare outputs on random small inputs. That covers the pattern and the pitfalls.