Largest Number After Removing K Digits
Updated
Problem statement
Given a decimal string num and an integer k, remove exactly k digits while preserving the relative order of the remaining digits.
Return the largest possible remaining integer as a string. Remove leading zeroes from the result. If no digit remains or the result contains only zeroes, return "0".
Function
largestAfterRemovingKDigits(num: String, k: int) → StringExamples
Example 1
num = "4902"k = 2return = "92"Remove 4 and 0. The two digits left keep their order and form the largest number you can get, which is the answer.
Example 2
num = "1432219"k = 3return = "4329"Remove the rest and keep the digits 4329, in their original order. No other choice of digits to keep gives a larger number, so that is the answer.
Example 3
num = "10"k = 2return = "0"Both digits are removed, so no digit is left. The function returns zero as a string.
Constraints
1 <= num.length <= 200000.numcontains only decimal digits.0 <= k <= num.length.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Remove K DigitsProcol · PHONE SCREEN
- Minimum Removal for Balanced ParenthesesMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Minimum Remove to Make Valid ParenthesesBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Smallest Number After Removing k DigitsAdobe · PHONE SCREENAdobe interview questions
- Remove Duplicate Letters for the Largest ResultAmazon · PHONE SCREEN
- Lexicographically Largest Distinct-Letter SubsequenceNavan · PHONE SCREENNavan interview questions
- Substring RemovalJP Morgan Chase · OAJP Morgan Chase interview questions
- Remove K DigitsThe D.E. Shaw Group · ONSITE INTERVIEWThe D.E. Shaw Group interview questions
- Remove K DigitsMorgan Stanley · ONSITE INTERVIEWMorgan Stanley interview questions
- Modify the StringYahoo · OAYahoo interview questions