Reported September 2026
Amazonmonotonic stack

Remove K Digits

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

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

The whole Amazon Remove K Digits question, reported in September 2026, hinges on one data structure: a stack that you keep in increasing order. If you've got an OA invite and 48 hours, that's the thing to lock in. It's a greedy problem wearing a string costume. The brute force of trying every combination dies fast at 10^5 digits. The stack version runs in one pass. If you blank on the monotonic idea mid-assessment, StealthCoder sits invisibly on your screen and hands you the working solution, so one lost thought doesn't sink the whole attempt.

The problem

You are given a string num that represents a non-negative integer, and an integer k.
Remove exactly k digits from num so the remaining digits stay in their original relative order and form the smallest possible integer.
Return that integer as a string. Do not keep leading zeros, except for the integer 0 itself.

Function
removeKdigits(num: String, k: int) → String

Examples
Example 1
num = "1432219"
k = 3
return = "1219"
Removing the digits 4, 3, and 2 from 1432219 leaves 1219, which is the smallest remaining integer.
Example 2
num = "10200"
k = 1
return = "200"
Removing the leading 1 leaves 0200, which becomes 200 after leading zeros are stripped.
Example 3
num = "10"
k = 2
return = "0"
Every digit is removed, so the result is 0.

Constraints
1 <= num.length <= 10^5.
1 <= k <= num.length.
num consists of digits only.
num has no leading zeros except when num is "0".

Reported by candidates. Source: FastPrep

Pattern and pitfall

Walk the digits left to right and keep a stack. While k is still above zero and the top of the stack is bigger than the current digit, pop it and decrement k. Then push the current digit. A bigger digit sitting before a smaller one is always worth removing, because it makes the number larger at a more significant position. After the loop, if k is still positive, the stack is non-decreasing, so chop k digits off the end. Then strip leading zeros and return "0" if nothing is left. The pitfalls are all edge cases: forgetting the leftover k, forgetting to strip zeros like in "10200", and returning an empty string when every digit is removed. Build the answer from a list and join it, not repeated string concatenation. If the edge cases slip away under pressure, StealthCoder is the hedge during the live OA.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Remove K Digits 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 StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as remove k digits. If you have time before the OA, drill that.

⏵ The honest play

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

Amazon 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.

Remove K Digits FAQ

What's the trick to Remove K Digits?+

Use a monotonic increasing stack. For each digit, pop the top while it's larger than the current digit and you still have removals left. This greedily clears the largest leading digits first. Push the current digit, and handle leftover k at the end.

How hard is this one really for the Amazon OA?+

It's medium. The idea is short once you see the stack, but the edge cases trip people up: leftover k, leading zeros, and the empty result. Most failed attempts are wrong on those, not on the main loop.

What's the time and space complexity?+

O(n) time and O(n) space. Each digit is pushed once and popped at most once, so the nested-looking while loop doesn't make it quadratic. That matters because num can be 10^5 digits long.

What happens if k is still greater than zero after the loop?+

The stack is then non-decreasing, so the largest digits sit at the end. Remove k digits from the tail. Example: "12345" with k=2 gives "123". Skipping this step is the most common wrong answer.

How do I prepare for this in 48 hours?+

Write the stack solution from memory twice. Then test it by hand on "1432219" with k=3, "10200" with k=1, and "10" with k=2. Those three cover the main loop, leading zeros, and the all-removed case. Do the same drill on similar monotonic stack problems.

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

OA at Amazon?
Invisible during screen share
Get it