402. Remove K Digits
Asked at Microsoft
Difficulty
Medium
Time
O(n)
Space
O(n)
Pattern
String
Companies
1
Problem
You are given a string num that holds a non-negative integer, and an integer k. Delete exactly k digits from num so that the digits that remain, read in their original order, form the smallest possible number.
You must keep the remaining digits in the same relative order — you can only remove digits, not rearrange them. Return the answer as a string, not as an integer.
The result must not have leading zeros. If every digit is removed, the answer is "0".
Delete k digits, keep the order
Constraints
- ●
1 <= k <= num.length <= 10^5 - ●
num contains only digit characters - ●
num has no leading zeros, except when num itself is "0"
Examples
num = "1432219", k = 3"1219"num = "10200", k = 1"200"num = "10", k = 2"0"Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Microsoft | Medium | View all Microsoft questions → |
How to Think About It
Brute force: try all combinations of removing k digits and pick the minimum (exponential).
Use a monotonic increasing stack to greedily remove larger digits first.
For each digit, while the stack top is larger and we still have removals left, pop from the stack.
After processing all digits, if removals remain, trim from the end (stack is non-decreasing).
Strip leading zeros and return the result or "0" if empty.
Optimal Approach
Use a monotonic increasing stack. Iterate through each digit: while the stack is not empty, the top is greater than the current digit, and we still have removals left, pop the top. Push the current digit. After the loop, if k is still positive, remove from the end. Convert the stack to a string, strip leading zeros, and return "0" if the result is empty. This is O(n) time and space.
Monotonic stack removes big digits first
What Trips People Up in Real Interviews
Clarify that the result should have no leading zeros.
Explain why greedy removal of larger preceding digits works.
Discuss why a monotonic stack is the right data structure.
Handle edge cases: k equals length of number, all zeros.
Mention that time complexity is O(n) since each element is pushed/popped once.
Solution Code
O(n) timedef removeKdigits(num, k):
stack = []
for d in num:
while stack and k > 0 and stack[-1] > d:
stack.pop()
k -= 1
stack.append(d)
while k > 0:
stack.pop()
k -= 1
result = ''.join(stack).lstrip('0')
return result if result else '0'Frequently Asked Questions
What is the Remove K Digits problem?
Given a string num representing a non-negative integer and an integer k, remove k digits from the number so that the remaining digits form the smallest possible integer. Return the result as a string.
How do you solve Remove K Digits?
The optimal approach runs in O(n) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Remove K Digits?
Remove K Digits is asked at Microsoft. It is a medium difficulty problem.
What are common mistakes on Remove K Digits?
- Clarify that the result should have no leading zeros.
- Explain why greedy removal of larger preceding digits works.
- Discuss why a monotonic stack is the right data structure.
- Handle edge cases: k equals length of number, all zeros.
- Mention that time complexity is O(n) since each element is pushed/popped once.