FastPrepRemove K Digits
Problems/Remove K Digits

Remove K Digits

Amazon logoAmazon● MediumINTERNONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report identifies LC 402 Remove K Digits: delete k digits from a number string so the remaining digits form the smallest possible integer. The judged core task matches the visible source at about 99%; bounds, leading-zero stripping, and the judged examples are specified for practice.

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 = 3return = "1219"

Removing the digits 4, 3, and 2 from 1432219 leaves 1219, which is the smallest remaining integer.

Example 2

num = "10200"k = 1return = "200"

Removing the leading 1 leaves 0200, which becomes 200 after leading zeros are stripped.

Example 3

num = "10"k = 2return = "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".

More Amazon problems

  • Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
See Amazon hiring insights
Saved
class Solution {
    public String removeKdigits(String num, int k) {
      // Write your code here.
    }
}
Checking account…