Remove K Digits
Updated
Problem statement
A nonnegative integer is represented by the decimal string num. Remove exactly k of its digits, preserving the order of every retained digit, to make the smallest possible integer.
Return its decimal representation without leading zeros. If no digit remains, or all retained digits are zero, return "0". Do not convert the complete input to a fixed-width integer.
Function
removeKdigits(num: String, k: int) → StringExamples
Example 1
num = "3241"k = 1return = "241"Removing 3 leaves 241. The other one-digit removals leave 341, 321, or 324, all larger.
Example 2
num = "10020"k = 1return = "20"Remove the leading 1 to leave 0020. Its canonical decimal representation is 20.
Example 3
num = "10"k = 2return = "0"Exactly two removals delete both digits. Return the required empty-result representation 0.
Constraints
- For this exercise, assume 1 <= num.length <= 10000.
- num contains only ASCII decimal digits.
- num has no leading zero unless num is exactly "0".
- 0 <= k <= num.length.
More Morgan Stanley Onsite Interview questions
- Generate Strings of Length at Least ThreeONSITE INTERVIEW · Seen Sep 2026
- Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
- Kth Largest Array ElementONSITE INTERVIEW · Seen Jul 2026
- Run-Length Encode Consecutive Letter RunsONSITE INTERVIEW · Seen Jul 2026
- Maximum Edge Removals for Two TravelersONSITE INTERVIEW · Seen Oct 2025
- Count Ideal ArraysONSITE INTERVIEW · Seen Aug 2023
- Distinct Palindrome PartitionsONSITE INTERVIEW · Seen Aug 2023
- LFU Cache OperationsONSITE INTERVIEW · Seen Aug 2023
- All-Pairs Shortest PathsOA · ONSITE INTERVIEW · Seen Feb 2022
- Fibonacci with Input ValidationONSITE INTERVIEW · Seen Feb 2022
Similar questions at other companies
- Remove K DigitsThe D.E. Shaw Group · ONSITE INTERVIEWThe D.E. Shaw Group interview questions
- Remove K DigitsAmazon · ONSITE INTERVIEW
- Minimum Removal for Balanced ParenthesesMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Largest K-Digit SubsequenceGoogle · ONSITE INTERVIEWGoogle interview questions
- Minimum Remove to Make Valid ParenthesesBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Remove K DigitsProcol · PHONE SCREEN
- 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
Interview reports that mention this question
Morgan Stanley interview experiences
- Morgan Stanley Apprenticeship and Tech Interviews: 6 Experiences
- Morgan Stanley New Grad Technology Analyst: 2 Experiences
- Morgan Stanley Intern Oldest Loops: 5 Experiences (Part 5 of 5)
- Morgan Stanley Intern OOP Loops: 5 Experiences (Part 4 of 5)
- Morgan Stanley Intern Earlier Loops: 5 Experiences (Part 3 of 5)