Parse And Format USDC Amounts Without Floating Point
Last updated: November 29, 2026
Quick Overview
Write parse_usdc(text) that converts a user-entered USDC amount such as '12.5' into integer base units (USDC has 6 decimals), rejecting anything malformed or too precise, and format_usdc(units) that converts back to a canonical string.
Circle
November 29, 20260
0
0 solved
Write parse_usdc(text) that converts a user-entered USDC amount such as '12.5' into integer base units (USDC has 6 decimals), rejecting anything malformed or too precise, and format_usdc(units) that converts back to a canonical string.
A warm-up in the style of a Circle phone screen for backend or wallet engineers, where every API amount ends up as an integer number of 6-decimal base units. The interviewer checks that you never touch floats with money, validate input strictly and round-trip cleanly.
What the Interviewer Expects
- Represents amounts as integers in base units, never floats
- Rejects signs, exponents, multiple dots and non-ASCII digits
- Rejects precision beyond 6 decimals unless the extra digits are zeros
- Formats with trailing zeros trimmed and no trailing dot
- Tests round-trips on boundary values like 0.000001
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
- How would you support EURC or a token with 18 decimals without duplicating code?
- Should '1.0000000' be accepted or rejected, and who decides?
- How would you handle locale formats such as '1.234,56' at the API edge?
- Where should validation live: client SDK, API gateway or ledger service?
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: a string from a user or API caller. Output: an
intin base units (1 USDC = 1,000,000 units). - Reject empty input, negatives, exponents, thousands separators, more than one dot, and non-ASC...
High-Level Design
Split on the single dot into whole and fractional parts, validate each as ASCII digits, right-pad the fraction to 6 digits, and combine with integer math. Formatting is divmod by 10**6 with the fr...