Remove k digits from a number string to get the smallest possible number

Quick Overview

Given a string of digits and a count k, remove exactly k digits so the remaining number is as small as possible, and return it without leading zeros. Tests greedy reasoning with a monotonic stack, plus careful handling of leftover removals and the all-zero case.

Remove k digits from a number string to get the smallest possible number

Company: DiDi

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a string `num` representing a non-negative integer and an integer `k`, return the smallest possible integer after removing exactly `k` digits from `num`. The digits that remain keep their original order. The result should not contain any leading zeros (except for the number `0` itself). If removing the digits leaves nothing, return `"0"`. ### Function Signature ```python def remove_k_digits(num: str, k: int) -> str: ``` ### Rules - Remove exactly `k` digits. The digits you keep stay in their original relative order. - Return the result as a decimal string with leading zeros stripped. - Return `"0"` when no digits remain, or when only zeros remain. ### Constraints - `1 <= k <= len(num) <= 10^5` - `num` consists of only digits. - `num` does not have any leading zeros except for the zero itself. ### Examples ```text Input: num = "1432219", k = 3 Output: "1219" ``` Remove the digits 4 and 3 and one of the two 2s to get 1219. Every result has four digits, and no choice of removals gives a four-digit result smaller than 1219. ```text Input: num = "10200", k = 1 Output: "200" ``` Remove the leading 1 to get "0200", then strip the leading zero to get "200". Removing any other digit leaves a number that starts with 1, which is larger. ```text Input: num = "10", k = 2 Output: "0" ``` Both digits are removed, so nothing remains and the answer is `"0"`.

Overview: Given a string of digits and a count k, remove exactly k digits so the remaining number is as small as possible, and return it without leading zeros. Tests greedy reasoning with a monotonic stack, plus careful handling of leftover removals and the all-zero case.

DiDi logo
DiDi
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a string num representing a non-negative integer and an integer k, return the smallest possible integer after removing exactly k digits from num. The digits that remain keep their original order.

The result should not contain any leading zeros (except for the number 0 itself). If removing the digits leaves nothing, return "0".

Function Signature

def remove_k_digits(num: str, k: int) -> str:

Rules

  • Remove exactly k digits. The digits you keep stay in their original relative order.
  • Return the result as a decimal string with leading zeros stripped.
  • Return "0" when no digits remain, or when only zeros remain.

Constraints

  • 1 <= k <= len(num) <= 10^5
  • num consists of only digits.
  • num does not have any leading zeros except for the zero itself.

Examples

Input: num = "1432219", k = 3
Output: "1219"

Remove the digits 4 and 3 and one of the two 2s to get 1219. Every result has four digits, and no choice of removals gives a four-digit result smaller than 1219.

Input: num = "10200", k = 1
Output: "200"

Remove the leading 1 to get "0200", then strip the leading zero to get "200". Removing any other digit leaves a number that starts with 1, which is larger.

Input: num = "10", k = 2
Output: "0"

Both digits are removed, so nothing remains and the answer is "0".