Remove K Digits
Problem
You are given a string num representing a non-negative integer with no sign, and an integer k. Delete exactly k digits from num so that the remaining digits, read in their original order, form the smallest possible number. Return that number as a string without leading zeros; if every digit is removed, return "0".
numhas between 1 and 200,000 digits, andkis between 1 and the number of digits.numconsists of digits only and has no leading zeros except when it is exactly"0".
Examples
num = "5723841", k = 3→"2341". Deleting5,7, and8leaves the smallest arrangement of four digits.num = "20500", k = 1→"500". Removing the2leaves0500, and leading zeros are stripped.num = "47", k = 2→"0". Everything is removed.
Hints
- Compare two numbers of equal length: what decides which is smaller?
- If a digit is larger than the digit right after it, removing it makes the number smaller. Which such digit should you remove first?
- Keep a stack of digits that is non-decreasing; while
k > 0and the incoming digit is smaller than the top, pop. Whateverkremains, trim from the end.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.