Quick Overview

Convert a 32-bit signed integer to its decimal string with commas between groups of three digits, so 1234 becomes 1,234, while handling negative numbers, zero and the most negative value. A variant of classic integer-to-string conversion that tests digit extraction and grouping edge cases.

Integer to String with Thousands Separators, Including Negative Numbers

Company: Hudson River Trading

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Convert a signed integer to its decimal string with a comma between each group of three digits, counting from the right; for example, 1234 becomes "1,234". Negative numbers keep a leading minus sign. This is a variant of the classic integer-to-string conversion (itoa), so the interviewer expects you to produce the digits yourself instead of calling a built-in number formatting routine. ### Function Signature ```python def format_with_commas(n: int) -> str: ``` ### Rules - The result is an optional `-` (only when `n < 0`), followed by the decimal digits of the absolute value of `n` with no leading zeros. - Starting from the rightmost digit, the digits are split into groups of three; the leftmost group has one to three digits. Groups are joined with a single `,`. - Zero is returned as `"0"`. The result contains no spaces, no `+` sign and no decimal point. ### Constraints - `-2^31 <= n <= 2^31 - 1` - For `n = -2^31` the absolute value is `2^31`, one more than the largest 32-bit signed integer, so an implementation in a fixed-width integer type must not negate it in 32 bits. ### Examples **Example 1** ```text Input: n = 1234 Output: "1,234" ``` **Example 2** ```text Input: n = -1000005 Output: "-1,000,005" ``` The groups after the first keep their zeros: `1`, `000`, `005`. **Example 3** ```text Input: n = -2147483648 Output: "-2,147,483,648" ``` This is the smallest allowed input.

Overview: Convert a 32-bit signed integer to its decimal string with commas between groups of three digits, so 1234 becomes 1,234, while handling negative numbers, zero and the most negative value. A variant of classic integer-to-string conversion that tests digit extraction and grouping edge cases.

Read the full Hudson River Trading Software Engineer interview experience this question came from

Given a signed integer `n`, return its decimal string with a comma between each group of three digits, counting from the right. For example, `1234` becomes `"1,234"`. Negative numbers keep a leading minus sign. This is a variant of the classic integer-to-string conversion (itoa): the interviewer expects you to produce the digits yourself instead of calling a built-in number formatting routine. Implement `format_with_commas(n)` and return the formatted string. ### Output rules - The result is an optional `-` (present only when `n < 0`), followed by the decimal digits of the absolute value of `n` with no leading zeros. - Starting from the rightmost digit, the digits are split into groups of three; the leftmost group has one to three digits. Groups are joined with a single `,`. - Zero is returned as `"0"`. The result contains no spaces, no `+` sign and no decimal point. ### Example 1 ```text Input: n = 1234 Output: "1,234" ``` ### Example 2 ```text Input: n = -1000005 Output: "-1,000,005" ``` The groups after the first keep their zeros: `1`, `000`, `005`. ### Constraints - `-2^31 <= n <= 2^31 - 1` - For `n = -2^31` the absolute value is `2^31`, which exceeds `2^31 - 1` (the largest 32-bit signed integer). The argument itself fits a 32-bit `int`, but an implementation in a fixed-width integer type must not negate it in 32 bits: in Java widen it to `long`, in C++ to `long long`, before taking the absolute value.

Constraints

  • -2^31 <= n <= 2^31 - 1
  • For n = -2^31 the absolute value is 2^31, which exceeds 2^31 - 1, so an implementation in a fixed-width integer type must not negate it in 32 bits (Java and C++ receive an int and should widen it to long / long long first).

Examples

Input: (0,)

Expected Output: '0'

Explanation: Minimum valid: zero is returned as the string 0 with no sign and no comma.

Input: (1,)

Expected Output: '1'

Explanation: Single positive digit, no comma.

Hints

  1. Deal with the minus sign separately from the digits: it belongs directly in front of the first group, never next to a comma.
  2. Groups are counted from the rightmost digit, so only the leftmost group may have fewer than three digits, and inner groups such as 000 keep their zeros.
  3. The absolute value of -2^31 does not fit in a 32-bit signed integer, so choose a wider type before negating.

Loading coding console...

Show the approach

Approach

Handle the sign and the magnitude separately. If n is 0, return "0" right away, because the digit loop would emit nothing. Otherwise record whether n is negative and take m = |n| in a type wide enough to hold 2^31 (Python int, JavaScript number, Java long, C++ long long), since negating -2^31 in 32 bits overflows. Repeatedly emit the last digit m % 10 as a character and divide m by 10, building the result from right to left. Keep a count of digits emitted so far; just before emitting a digit when that count is a positive multiple of 3, emit a comma first.

Invariant: after k digits have been emitted, the buffer holds, in reverse order, the last k digits of |n| split into groups of three counted from the right, with exactly one comma between consecutive groups. A comma is written only immediately before another digit, so the result never starts or ends with a comma, and the leftmost group has one to three digits. The loop stops as soon as m reaches 0, so there are no leading zeros, while zeros inside the number (the 000 and 005 of 1,000,005) are emitted as ordinary digits. Finally a '-' is appended when n was negative and the buffer is reversed, so the sign sits directly before the first group and never next to a comma.

Edge cases: n = 0; one- to three-digit values, which get no comma; values just across a group boundary such as 999 vs 1000 and 999999 vs 1000000; and n = -2^31, whose absolute value 2^31 does not fit a 32-bit signed integer.

Time complexity:
O(log |n|)
Space complexity:
O(log |n|)