Remove K Digits for the Smallest Number
Updated
Problem statement
You are given a non-negative integer as a string num and an integer k.
Delete exactly k digits from num. The digits you keep stay in their original order. Return the smallest number you can get, as a string.
Write the result without leading zeros. If no digits are left, or only zeros are left, return "0".
Function
removeKDigits(num: String, k: int) → StringExamples
Example 1
num = "4205839"k = 3return = "539"Three digits must go, so four stay. Deleting 4 and 2 brings the 0 to the front, and a leading zero is not written, so the result has at most three digits. That leaves 0 5 8 3 9 with one more digit to delete. Deleting 8, the first digit that is bigger than the digit after it, gives 0539, written "539".
Example 2
num = "30200"k = 1return = "200"Deleting the 3 leaves 0200, which is written "200". Deleting any other digit keeps the 3 in front and leaves a four-digit number, which is bigger.
Example 3
num = "71"k = 2return = "0"Both digits are deleted. Nothing is left, so the answer is "0".
Constraints
1 <= k <= num.length <= 10^4numconsists only of the digits0to9.numhas no leading zeros, except thatnummay be exactly"0".
More Nutanix Online Assessment questions
- Good SubsequencesOA · Seen Jul 2026
- Min Cost to Connect All PointsOA · Seen Jun 2026
- Digit Changes for a Multiple of 9OA · Seen Aug 2025
- Gas Station Loop StartOA · Seen Aug 2025
- Number of Players (for MTS2)OA · Seen Oct 2024
- Work Schedules (for MTS2)OA · Seen Oct 2024
- Minimum Operations to Convert ArrayOA · Seen Apr 2024
- No Adjacent CharactersOA · Seen Apr 2024
- Build a BST From a ListOA · Seen Nov 2022
- Delete Even-Valued Nodes From a Linked ListOA · Seen Nov 2022
Similar questions at other companies
- Substring RemovalJP Morgan Chase · OAJP Morgan Chase interview questions
- Modify the StringYahoo · OAYahoo interview questions
- Lexicographically Minimum Stack EncryptionRippling · OARippling interview questions
- Shortest String After Erasing Equal PairsMarshall Wace · OAMarshall Wace interview questions
- Minimum Insertions to Balance a Parentheses StringIBM · OAIBM interview questions
- Largest SubsequencesCashfree Payments · OA
- Good Binary StringsBNP Paribas · OA
- Remove K DigitsProcol · PHONE SCREEN
- Remove K DigitsThe D.E. Shaw Group · ONSITE INTERVIEWThe D.E. Shaw Group interview questions
- Remove K DigitsAmazon · ONSITE INTERVIEW