Smallest Tracking Number After Deleting K Digits

Last updated: November 29, 2026

Quick Overview

A tracking number is a string of digits. Remove exactly k digits so the remaining digits, in their original order, form the smallest possible number. Return it without leading zeros, or '0' if nothing remains.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


A tracking number is a string of digits. Remove exactly k digits so the remaining digits, in their original order, form the smallest possible number. Return it without leading zeros, or '0' if nothing remains.

An Amazon onsite greedy plus monotonic stack question. The interviewer looks for the insight that the first position where a digit is larger than its successor is where a deletion helps most, and expects careful handling of leftover k and leading zeros.

What the Interviewer Expects
  • States the greedy rule: delete a digit greater than the next digit
  • Implements with an increasing stack
  • Removes remaining k digits from the end
  • Strips leading zeros and returns '0' when empty
  • Gives O(n) time
Key Topics to Cover
monotonic stack
greedy
string processing
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
  • Make the largest number instead.
  • Remove digits so the result is divisible by 3.
  • Merge two digit strings into the largest number of length k.
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: digit string num (length up to 10^5) and 0 <= k <= len(num). Output: smallest resulting number as a string....

High-Level Design

Among numbers of the same length, a smaller digit earlier wins. Scanning left to right, if the previous kept digit is larger than the current digit, deleting the previous one makes the number smaller,...


Submit Your Answer
Markdown supported