Smallest Promo Code After Deleting Exactly K Digits

Last updated: November 29, 2026

Quick Overview

Given a numeric promo code string and an integer k, delete exactly k digits so the remaining number is as small as possible, and return it without leading zeros (or "0" if empty).

ByteDance
Coding & Algorithms
Software Engineer
ByteDance
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Medium

0

0

0 solved


Given a numeric promo code string and an integer k, delete exactly k digits so the remaining number is as small as possible, and return it without leading zeros (or "0" if empty).

Douyin e-commerce campaigns generate numeric codes, and this question shows up in ByteDance online assessments as a monotonic stack exercise. The grader checks the greedy insight (remove a digit when a smaller one follows) and the leftover edge cases with zeros and unused deletions.

What the Interviewer Expects
  • Use a monotonic increasing stack
  • Pop larger previous digits while deletions remain
  • Remove leftover deletions from the tail
  • Strip leading zeros and return '0' for empty results
  • Justify the greedy choice
Key Topics to Cover
monotonic stack
greedy
string manipulation
lexicographic order
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if you must maximize the number instead?
  • How would you solve it if digits come from a stream and k is fixed?
  • Pick k digits from two arrays to form the largest number while keeping order in each.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: a digit string of length n and 0 <= k <= n. Output: the smallest possible number as a string after deleting exactly k digits, preserving relative order. Leading zeros are dropped....

High-Level Design

Scanning left to right, the most significant position matters most. If the current digit is smaller than the previous kept digit, deleting the previous digit makes the number smaller, so pop while we ...


Submit Your Answer
Markdown supported