Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
Given an integer n, add a dot (".") as the thousands separator and return it in string format.
Example 1:
Input: n = 987 Output: "987"
Example 2:
Input: n = 1234 Output: "1.234"
Constraints:
0 <= n <= 231 - 1Problem summary: Given an integer n, add a dot (".") as the thousands separator and return it in string format.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
987
1234
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1556: Thousand Separator
class Solution {
public String thousandSeparator(int n) {
int cnt = 0;
StringBuilder ans = new StringBuilder();
while (true) {
int v = n % 10;
n /= 10;
ans.append(v);
++cnt;
if (n == 0) {
break;
}
if (cnt == 3) {
ans.append('.');
cnt = 0;
}
}
return ans.reverse().toString();
}
}
// Accepted solution for LeetCode #1556: Thousand Separator
func thousandSeparator(n int) string {
cnt := 0
ans := []byte{}
for {
v := n % 10
n /= 10
ans = append(ans, byte('0'+v))
if n == 0 {
break
}
cnt++
if cnt == 3 {
ans = append(ans, '.')
cnt = 0
}
}
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
ans[i], ans[j] = ans[j], ans[i]
}
return string(ans)
}
# Accepted solution for LeetCode #1556: Thousand Separator
class Solution:
def thousandSeparator(self, n: int) -> str:
cnt = 0
ans = []
while 1:
n, v = divmod(n, 10)
ans.append(str(v))
cnt += 1
if n == 0:
break
if cnt == 3:
ans.append('.')
cnt = 0
return ''.join(ans[::-1])
// Accepted solution for LeetCode #1556: Thousand Separator
struct Solution;
impl Solution {
fn thousand_separator(mut n: i32) -> String {
if n == 0 {
return "0".to_string();
}
let mut stack = vec![];
let mut count = 0;
while n > 0 {
if count % 3 == 0 && count > 0 {
stack.push('.');
}
stack.push((b'0' + (n % 10) as u8) as char);
n /= 10;
count += 1;
}
stack.into_iter().rev().collect()
}
}
#[test]
fn test() {
let n = 987;
let res = "987".to_string();
assert_eq!(Solution::thousand_separator(n), res);
let n = 1234;
let res = "1.234".to_string();
assert_eq!(Solution::thousand_separator(n), res);
let n = 123456789;
let res = "123.456.789".to_string();
assert_eq!(Solution::thousand_separator(n), res);
let n = 0;
let res = "0".to_string();
assert_eq!(Solution::thousand_separator(n), res);
}
// Accepted solution for LeetCode #1556: Thousand Separator
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1556: Thousand Separator
// class Solution {
// public String thousandSeparator(int n) {
// int cnt = 0;
// StringBuilder ans = new StringBuilder();
// while (true) {
// int v = n % 10;
// n /= 10;
// ans.append(v);
// ++cnt;
// if (n == 0) {
// break;
// }
// if (cnt == 3) {
// ans.append('.');
// cnt = 0;
// }
// }
// return ans.reverse().toString();
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.