Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
Given an integer n represented as a string, return the smallest good base of n.
We call k >= 2 a good base of n, if all digits of n base k are 1's.
Example 1:
Input: n = "13" Output: "3" Explanation: 13 base 3 is 111.
Example 2:
Input: n = "4681" Output: "8" Explanation: 4681 base 8 is 11111.
Example 3:
Input: n = "1000000000000000000" Output: "999999999999999999" Explanation: 1000000000000000000 base 999999999999999999 is 11.
Constraints:
n is an integer in the range [3, 1018].n does not contain any leading zeros.Problem summary: Given an integer n represented as a string, return the smallest good base of n. We call k >= 2 a good base of n, if all digits of n base k are 1's.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Binary Search
"13"
"4681"
"1000000000000000000"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #483: Smallest Good Base
class Solution {
public String smallestGoodBase(String n) {
long num = Long.parseLong(n);
for (int len = 63; len >= 2; --len) {
long radix = getRadix(len, num);
if (radix != -1) {
return String.valueOf(radix);
}
}
return String.valueOf(num - 1);
}
private long getRadix(int len, long num) {
long l = 2, r = num - 1;
while (l < r) {
long mid = l + r >>> 1;
if (calc(mid, len) >= num)
r = mid;
else
l = mid + 1;
}
return calc(r, len) == num ? r : -1;
}
private long calc(long radix, int len) {
long p = 1;
long sum = 0;
for (int i = 0; i < len; ++i) {
if (Long.MAX_VALUE - sum < p) {
return Long.MAX_VALUE;
}
sum += p;
if (Long.MAX_VALUE / p < radix) {
p = Long.MAX_VALUE;
} else {
p *= radix;
}
}
return sum;
}
}
// Accepted solution for LeetCode #483: Smallest Good Base
// Auto-generated Go example from java.
func exampleSolution() {
}
// Reference (java):
// // Accepted solution for LeetCode #483: Smallest Good Base
// class Solution {
// public String smallestGoodBase(String n) {
// long num = Long.parseLong(n);
// for (int len = 63; len >= 2; --len) {
// long radix = getRadix(len, num);
// if (radix != -1) {
// return String.valueOf(radix);
// }
// }
// return String.valueOf(num - 1);
// }
//
// private long getRadix(int len, long num) {
// long l = 2, r = num - 1;
// while (l < r) {
// long mid = l + r >>> 1;
// if (calc(mid, len) >= num)
// r = mid;
// else
// l = mid + 1;
// }
// return calc(r, len) == num ? r : -1;
// }
//
// private long calc(long radix, int len) {
// long p = 1;
// long sum = 0;
// for (int i = 0; i < len; ++i) {
// if (Long.MAX_VALUE - sum < p) {
// return Long.MAX_VALUE;
// }
// sum += p;
// if (Long.MAX_VALUE / p < radix) {
// p = Long.MAX_VALUE;
// } else {
// p *= radix;
// }
// }
// return sum;
// }
// }
# Accepted solution for LeetCode #483: Smallest Good Base
class Solution:
def smallestGoodBase(self, n: str) -> str:
def cal(k, m):
p = s = 1
for i in range(m):
p *= k
s += p
return s
num = int(n)
for m in range(63, 1, -1):
l, r = 2, num - 1
while l < r:
mid = (l + r) >> 1
if cal(mid, m) >= num:
r = mid
else:
l = mid + 1
if cal(l, m) == num:
return str(l)
return str(num - 1)
// Accepted solution for LeetCode #483: Smallest Good Base
/**
* [0483] Smallest Good Base
*
* Given an integer n represented as a string, return the smallest good base of n.
* We call k >= 2 a good base of n, if all digits of n base k are 1's.
*
* Example 1:
*
* Input: n = "13"
* Output: "3"
* Explanation: 13 base 3 is 111.
*
* Example 2:
*
* Input: n = "4681"
* Output: "8"
* Explanation: 4681 base 8 is 11111.
*
* Example 3:
*
* Input: n = "1000000000000000000"
* Output: "999999999999999999"
* Explanation: 1000000000000000000 base 999999999999999999 is 11.
*
*
* Constraints:
*
* n is an integer in the range [3, 10^18].
* n does not contain any leading zeros.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/smallest-good-base/
// discuss: https://leetcode.com/problems/smallest-good-base/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn smallest_good_base(n: String) -> String {
let n = n.parse::<i64>().unwrap();
let longest = (n as f64).log(2.0) as u32 + 1;
for m in (2..=longest).rev() {
let k: i64 = f64::powf(n as f64, 1.0 / (m as f64 - 1.0)) as i64;
if Self::check(n, k, m) {
return k.to_string();
}
}
(n - 1).to_string()
}
fn check(n: i64, k: i64, m: u32) -> bool {
let k_pow_m = (k as i128).pow(m);
(k_pow_m - 1) % (k as i128 - 1) == 0 && n as i128 == (k_pow_m - 1) / (k as i128 - 1)
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_0483_example_1() {
let n = "13".to_string();
let result = "3".to_string();
assert_eq!(Solution::smallest_good_base(n), result);
}
#[test]
fn test_0483_example_2() {
let n = "4681".to_string();
let result = "8".to_string();
assert_eq!(Solution::smallest_good_base(n), result);
}
#[test]
fn test_0483_example_3() {
let n = "1000000000000000000".to_string();
let result = "999999999999999999".to_string();
assert_eq!(Solution::smallest_good_base(n), result);
}
}
// Accepted solution for LeetCode #483: Smallest Good Base
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #483: Smallest Good Base
// class Solution {
// public String smallestGoodBase(String n) {
// long num = Long.parseLong(n);
// for (int len = 63; len >= 2; --len) {
// long radix = getRadix(len, num);
// if (radix != -1) {
// return String.valueOf(radix);
// }
// }
// return String.valueOf(num - 1);
// }
//
// private long getRadix(int len, long num) {
// long l = 2, r = num - 1;
// while (l < r) {
// long mid = l + r >>> 1;
// if (calc(mid, len) >= num)
// r = mid;
// else
// l = mid + 1;
// }
// return calc(r, len) == num ? r : -1;
// }
//
// private long calc(long radix, int len) {
// long p = 1;
// long sum = 0;
// for (int i = 0; i < len; ++i) {
// if (Long.MAX_VALUE - sum < p) {
// return Long.MAX_VALUE;
// }
// sum += p;
// if (Long.MAX_VALUE / p < radix) {
// p = Long.MAX_VALUE;
// } else {
// p *= radix;
// }
// }
// return sum;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.