Delete K Digits From a Build Number to Make It Smallest
Last updated: November 29, 2026
Quick Overview
A release build number is given as a digit string. Remove exactly k digits, keeping the order of the rest, so the resulting number is as small as possible, and return it without leading zeros.
0
0
0 solved
A release build number is given as a digit string. Remove exactly k digits, keeping the order of the rest, so the resulting number is as small as possible, and return it without leading zeros.
Google technical screens use monotonic-stack greedy questions to see whether a candidate can reason locally about digit positions. The interviewer checks the leading-digit argument and careful handling of leading zeros and leftover deletions.
What the Interviewer Expects
- Argue that a larger digit before a smaller one should be removed first
- Implement with a monotonic increasing stack
- Spend leftover deletions from the end
- Strip leading zeros and return "0" for an empty result
- O(n) time
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
- Make the number as large as possible instead.
- Remove exactly k digits so the result is divisible by 3. How does it change?
- Two build numbers: choose k digits total from both to form the largest number.
- Stream the digits and output the answer with bounded 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: num (digit string, length up to 10^5) and k with 0 <= k <= len(num). Output: the smallest possible remaining number as a string, no leading zeros, "0" if nothing is left or only zeros r...
High-Level Design
The leftmost digit matters most. Scanning left to right, if the current digit is smaller than the previous kept digit, deleting that previous digit makes the number smaller at a more significant posit...