Remove K Digits
Problem Statement
You are given a non-negative number as a string num, and an integer k.
Remove exactly k digits from num. The other digits stay in their original order. Return the smallest number you can get, as a string.
The result should not have leading zeros. If nothing is left, or only zeros are left, return "0".
A digit string num and an integer k.
The smallest possible number after removing k digits, as a string without leading zeros.
Remove 4, 3 and the first 2. What is left is 1219.
Remove the 1 to get "0200". Leading zeros are dropped, so the answer is "200".
Both digits are removed. Nothing is left, so the answer is "0".
- • 1 ≤ k ≤ num.length ≤ 10^5
- • `num` has only digits
- • `num` has no leading zeros, except when the number is just "0"
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
k times: scan for the first digit bigger than the next one and erase it
Pop a bigger top while a smaller digit arrives (and k > 0); trim the tail; strip zeros
Make the k removals one by one. Each time, scan from the left for the first digit that is bigger than the digit right after it, and erase it. If the digits never go down, erase the last digit. At the end, strip leading zeros.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Monotonic Stack translate into production architectures and low-level memory mechanics.