Smallest Build Number After Deleting K Digits
Last updated: November 29, 2026
Quick Overview
Given a build number as a string of digits and an integer k, delete exactly k digits so the remaining number is as small as possible, and return it without leading zeros ("0" if nothing remains).
Microsoft
November 29, 20260
0
0 solved
Given a build number as a string of digits and an integer k, delete exactly k digits so the remaining number is as small as possible, and return it without leading zeros ("0" if nothing remains).
Microsoft online assessments include greedy plus stack questions like this one. The interviewer is checking that you see why removing an earlier larger digit beats any later removal, and that you handle leading zeros and leftover deletions.
What the Interviewer Expects
- Argue the greedy rule: remove a digit when the next digit is smaller
- Implement with a monotonic non-decreasing stack
- Trim from the end when deletions remain
- Strip leading zeros and return "0" for empty results
- Run in O(n)
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What if you need the largest possible number instead?
- What if you must keep the relative order but can choose which k digits to keep from two numbers?
- How would you handle a very long string that does not fit in memory?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: digit string
digitsand integer k with0 <= k <= len(digits). - Remove exactly k digits keeping the order of the rest.
- Output: smallest result as a string, no leading zeros, "0" if empt...
High-Level Design
The leftmost digit matters most. If some digit is followed by a smaller digit, deleting it shrinks the number at the earliest possible position, and no deletion further right can compensate. So we sca...