One Digit Swap to Maximize a Storage Quota Number
Last updated: November 29, 2026
Quick Overview
A Google One storage quota is a non-negative integer. You may swap two of its digits at most once; return the largest number you can get.
0
0
0 solved
A Google One storage quota is a non-negative integer. You may swap two of its digits at most once; return the largest number you can get.
A short phone-screen problem in Google's style that rewards precise greedy reasoning about the most significant position. The interviewer checks that you swap with the last occurrence of the largest later digit, and that you avoid an O(n^2) brute force.
What the Interviewer Expects
- Record the last index of each digit
- Find the leftmost position that has a larger digit later
- Swap with the largest such digit, choosing its last occurrence
- Return the input unchanged when it is already maximal
- 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
- Minimize the number instead, without creating a leading zero.
- Allow up to k swaps.
- Swaps only between adjacent digits, at most k of them.
- The number is given as a huge string; does anything change?
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: integer num >= 0. Output: the maximum value obtainable with zero or one swap of two digits....
High-Level Design
The most significant position we can improve decides the answer. Scan digits left to right; at position i, check whether some digit larger than digits[i] appears later. The first such i is where to ...