Best Way to Pack
Learn this problemProblem statement
The team at an Amazon warehouse is given a task to optimize the packing of a set of boxes with different IDs. Each box is labeled with an ID, and these boxes are currently arranged in a single row from left to right, where the ID of the i-th box is represented by the string s_id consisting of digits from 0 to 9 inclusive.
To make the packing more efficient, the team can perform the following operation any (possibly zero) number of times:
- Choose an index
iand remove the digits_id[i]. Then insert the box with IDmin(s_id[i] + 1, 9)on any position (at the beginning, at the end, or in between any two adjacent boxes) in the row.
Given a string s_id, find the lexicographically minimal string of boxes using these operations.
Note: A string X is lexicographically smaller than a string Y of the same length if and only if, in the first position where X and Y differ, the string X has a smaller digit than the corresponding digit in Y.
Function
bestWayToPack(id: String) → StringExamples
Example 1
id = "26547"return = "24677"Example 2
id = "34892"return = "24677"Constraints
Constraints:
s_idconsists only of digits from0to9inclusive.- Each operation removes exactly one digit and inserts exactly one digit, so the length of the string is preserved.
- The inserted digit for a removed digit
dismin(d + 1, 9).
More Amazon problems
- Secure Maximum DeliveriesOA · Seen Jul 2026
- Find Median from Data StreamONSITE INTERVIEW · Seen Jul 2026
- Handwritten SigmoidPHONE SCREEN · Seen Jul 2026
- Handwritten SoftmaxPHONE SCREEN · Seen Jul 2026
- Koko Eating BananasONSITE INTERVIEW · Seen Jul 2026
- Loyal Customers Across Two DaysONSITE INTERVIEW · Seen Jul 2026
- Maximum System Memory CapacityOA · Seen Jul 2026
- Package Delivery SystemOA · Seen Jul 2026