Format Google Drive Storage Byte Counts as Human-Readable Sizes
Last updated: November 29, 2026
Quick Overview
Given a non-negative byte count, return a string like "999 B", "1.5 KB", or "2 GB" using decimal units (powers of 1000), one decimal place at most, trailing ".0" removed, and correct promotion when rounding reaches 1000 of a unit.
0
0
0 solved
Given a non-negative byte count, return a string like "999 B", "1.5 KB", or "2 GB" using decimal units (powers of 1000), one decimal place at most, trailing ".0" removed, and correct promotion when rounding reaches 1000 of a unit.
A small Google Drive or Photos UI question about showing storage quota. It looks like a formatting one-liner, but the interviewer is watching for the rounding boundary (999,950 bytes must not show as "1000 KB"), unit overflow at the top, and negative values.
What the Interviewer Expects
- Picks the unit by repeated division with a cap at the largest unit
- Rounds to one decimal and strips a trailing .0
- Promotes to the next unit when rounding produces 1000
- Keeps plain bytes as integers
- Mentions binary (KiB) versus decimal units as a product decision
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
- Support binary units (KiB, MiB) behind a flag and explain when each is right.
- Localize the output for locales that use a decimal comma.
- Parse the formatted string back to bytes; is it lossless?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Units: B, KB, MB, GB, TB, PB with factor 1000 (Drive quota pages use decimal units).
- Under 1000 bytes: integer with
B. - Otherwise: one decimal place, drop
.0, so1,000,000is1 MBand `1,...
High-Level Design
Divide by 1000 until the value drops below 1000 or we run out of units. Format to one decimal. The subtle case is rounding: 999,950 bytes is 999.95 KB, which formats as 1000.0. Detect that exact...