Format Like and View Counts as Compact Labels Like 1.2M
Last updated: November 29, 2026
Quick Overview
Write `compact_count(n)` that turns a non-negative count into the short label shown under TikTok videos: plain digits below 1000, otherwise one decimal place with K, M or B, truncated (never rounded up) and with a trailing `.0` dropped.
TikTok
November 29, 20260
0
0 solved
Write `compact_count(n)` that turns a non-negative count into the short label shown under TikTok videos: plain digits below 1000, otherwise one decimal place with K, M or B, truncated (never rounded up) and with a trailing `.0` dropped.
Every video, comment and profile on TikTok shows compact counts, and this quick phone screen problem tests numeric care rather than algorithms. The interviewer checks that you avoid floating point artifacts, never display a number larger than the real one, and handle unit boundaries like 999,999.
What the Interviewer Expects
- Clarifies truncation versus rounding and explains why truncation is safer for social proof numbers
- Uses integer arithmetic, not float formatting
- Correct at boundaries such as 999, 1000, 999999 and 1000000
- Drops trailing .0
- Rejects or handles negative input
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
- Localize it: Chinese uses 万 (10^4) and 亿 (10^8) units; how would you generalize?
- What if product wants rounding, and how do you avoid showing 1000K?
- Should this run on the client or the server, and why?
- How would you format counts above 999.9B?
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 n >= 0. Below 1000 return digits. Otherwise pick the largest unit (B = 10^9, M = 10^6, K = 10^3) not exceeding n, show one decimal place truncated, and drop .0. So 1299 is 1.2K, 105...
High-Level Design
Iterate units from largest to smallest; the first with n >= size wins. Compute tenths = n * 10 // size with integer division, which truncates, then split into whole and fractional digit with `divm...