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.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
Given an integer num, return a string of its base 7 representation.
Example 1:
Input: num = 100 Output: "202"
Example 2:
Input: num = -7 Output: "-10"
Constraints:
-107 <= num <= 107Problem summary: Given an integer num, return a string of its base 7 representation. Example 1: Input: num = 100 Output: "202" Example 2: Input: num = -7 Output: "-10"
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
100
-7
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #504: Base 7
class Solution {
public String convertToBase7(int num) {
if (num == 0) {
return "0";
}
if (num < 0) {
return "-" + convertToBase7(-num);
}
StringBuilder sb = new StringBuilder();
while (num != 0) {
sb.append(num % 7);
num /= 7;
}
return sb.reverse().toString();
}
}
// Accepted solution for LeetCode #504: Base 7
func convertToBase7(num int) string {
if num == 0 {
return "0"
}
if num < 0 {
return "-" + convertToBase7(-num)
}
ans := []byte{}
for num != 0 {
ans = append([]byte{'0' + byte(num%7)}, ans...)
num /= 7
}
return string(ans)
}
# Accepted solution for LeetCode #504: Base 7
class Solution:
def convertToBase7(self, num: int) -> str:
if num == 0:
return '0'
if num < 0:
return '-' + self.convertToBase7(-num)
ans = []
while num:
ans.append(str(num % 7))
num //= 7
return ''.join(ans[::-1])
// Accepted solution for LeetCode #504: Base 7
impl Solution {
pub fn convert_to_base7(mut num: i32) -> String {
if num == 0 {
return String::from("0");
}
let mut res = String::new();
let is_minus = num < 0;
if is_minus {
num = -num;
}
while num != 0 {
res.push_str((num % 7).to_string().as_str());
num /= 7;
}
if is_minus {
res.push('-');
}
res.chars().rev().collect()
}
}
// Accepted solution for LeetCode #504: Base 7
function convertToBase7(num: number): string {
if (num == 0) {
return '0';
}
let res = '';
const isMinus = num < 0;
if (isMinus) {
num = -num;
}
while (num != 0) {
const r = num % 7;
res = r + res;
num = (num - r) / 7;
}
return isMinus ? '-' + res : res;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
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.