Thousand Separator — Solution in Python, Java & C++
Thousand Separator solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on strings.
Thousand Separator is an easy coding problem on strings, asked in Amazon and TCS interviews.
- Difficulty: Easy
- Topics: Strings
- Asked at: Amazon, TCS
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given a non-negative integer n, return its decimal representation with a dot (.) inserted as the thousands separator — every three digits, counting from the right.
Example 1
Input: n = 987
Output: 987
Explanation: Fewer than four digits, so no separator is added.
Example 2
Input: n = 1234
Output: 1.234
Example 3
Input: n = 123456789
Output: 123.456.789
Constraints
0 <= n <= 2147483647
Hints for Thousand Separator
Hint 1
Group from the right, not the left — the leftmost group may be shorter than three digits.
Hint 2
Working left to right, insert a dot before index
iwhen(len - i)is a positive multiple of 3.Hint 3
No separator ever precedes the first digit.
How to solve Thousand Separator
Grouping is defined from the right, so the condition for a separator at a left-to-right position depends on the number of digits remaining, not on the position itself.
Approach
- Convert
nto its decimal string. - Walk the digits left to right.
- Before writing the digit at index
i, emit a dot wheni > 0and(len - i) % 3 == 0. - Append the digit and continue.
Why it works
len - i counts the digits from i to the end. When that count is a multiple of three, position i starts a fresh group — exactly where the separator belongs. The i > 0 guard prevents a leading dot when the length itself is a multiple of three.
Complexity
- Time —
O(d) in the digit count - Space —
O(d)
Pitfalls
- Inserting every third character from the left produces
123.456.789correctly by luck but breaks on1234(giving123.4). - Dropping the
i > 0guard yields a leading dot for numbers whose length is divisible by 3. n = 0must return"0".
Reference solution
Python
def thousandSeparator(n: int) -> str:
s = str(n)
out = []
for i, c in enumerate(s):
if i > 0 and (len(s) - i) % 3 == 0:
out.append('.')
out.append(c)
return ''.join(out)Java
public static String thousandSeparator(int n) {
String s = Integer.toString(n);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (i > 0 && (s.length() - i) % 3 == 0) sb.append('.');
sb.append(s.charAt(i));
}
return sb.toString();
}C++
string thousandSeparator(int n) {
string s = to_string(n);
string out;
for (int i = 0; i < (int) s.size(); i++) {
if (i > 0 && ((int) s.size() - i) % 3 == 0) out += '.';
out += s[i];
}
return out;
}JavaScript
var thousandSeparator = function(n) {
const s = String(n);
let out = "";
for (let i = 0; i < s.length; i++) {
if (i > 0 && (s.length - i) % 3 === 0) out += ".";
out += s.charAt(i);
}
return out;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 515 strings problems · the whole catalogue
Learn the technique: Strings