Base 7 - Problem
Base 7 Number System Conversion

Given an integer num, convert it to its base 7 representation and return it as a string.

In the base 7 number system, we use digits 0 through 6 to represent numbers. For example:
• Decimal 7 becomes "10" in base 7
• Decimal 49 becomes "100" in base 7

Your task is to implement the conversion algorithm that works for both positive and negative integers.

Input & Output

example_1.py — Basic Positive Number
$ Input: num = 100
Output: "202"
💡 Note: 100 in base 10 equals 2×7² + 0×7¹ + 2×7⁰ = 98 + 0 + 2 = 100, so the base 7 representation is "202"
example_2.py — Negative Number
$ Input: num = -7
Output: "-10"
💡 Note: 7 in base 10 equals 1×7¹ + 0×7⁰ = 7, so -7 becomes "-10" in base 7
example_3.py — Zero Edge Case
$ Input: num = 0
Output: "0"
💡 Note: Zero is represented as "0" in any base system

Constraints

  • -107 ≤ num ≤ 107
  • The input is guaranteed to fit in a 32-bit signed integer

Visualization

Tap to expand
Base 7 Conversion: 100₁₀ → 202₇100100 ÷ 7 = 14remainder: 2214 ÷ 7 = 2remainder: 002 ÷ 7 = 0remainder: 22Final Answer"202"Read upward →reverse order
Understanding the Visualization
1
Start with 100
We want to convert decimal 100 to base 7
2
Divide by 7
100 ÷ 7 = 14 remainder 2 (rightmost digit)
3
Continue dividing
14 ÷ 7 = 2 remainder 0 (middle digit)
4
Final division
2 ÷ 7 = 0 remainder 2 (leftmost digit)
5
Read remainders
Reading from bottom to top: 2-0-2 = "202"
Key Takeaway
🎯 Key Insight: The remainders from repeated division by 7, read in reverse order, give us the base-7 representation
Asked in
Google 25 Amazon 18 Microsoft 15 Apple 12
28.5K Views
Medium Frequency
~12 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen