At first glance, Base 7 looks like a trivial number conversion task. In interviews, however, it is often used to assess whether candidates understand positional number systems, remainder-based decomposition, and edge-case handling especially around negative numbers and zero without relying on built-in shortcuts.
Problem statement
You are given an integer num. Your task is to return its base 7 representation as a string.
Constraints:
- −107 <=
num<= 107
Examples
| Input | Output | Explanation |
| 45 | “63” | Dividing 45 by 7 gives remainders 3, then 6. Reading these remainders in reverse order produces the base-7 number 63. |
| -28 | “-40” | The absolute value 28 converts to 40 in base 7 (since 28 is exactly 4 × 7). The negative sign is added back at the end. |
| 6 | “6” | As 6 is smaller than 7, it fits directly as a single base-7 digit without any division. |
Intuition
Every positional number system follows the same principle.
The key insight is that the remainder after dividing by 7 gives the next digit, while integer division by 7 removes the least significant digit. Because of this, digits are produced from right to left.
Once this mental model is clear, the implementation becomes straightforward.
Base conversion using repeated division
For this problem, we can use the standard base conversion algorithm, commonly known as the repeated division (division–remainder) method.
Theis algorithm works by repeatedly dividing the number by the target base and recording the remainders. Each remainder corresponds to a digit in the new base, starting from the least significant digit. The process continues until the number is reduced to zero.
This method is canonical, efficient, and applies uniformly to conversions into any base. The only adjustment needed is to reverse the collected digits at the end, since they are generated in reverse order.
Step-by-step algorithm
- If
num == 0, return"0"immediately. - Record whether the number is negative.
- Convert
numto its absolute value. - Initialize an empty list
digitsto store digits. - While
num > 0:- Append
num % 7to the list. - Update
num = num // 7.
- Append
- Reverse the
digitlist and join it into a string. - Add a
'-'sign if the original number was negative.
Let’s look at the following illustration to get a better understanding of the solution:
Code implementation
Let’s look at the code for this solution below:
Code for the Base 7 problem
Time complexity
Each division by 7 removes one base-7 digit. The number of iterations is proportional to the number of digits in the base-7 representation. So, the overall time complexity of this algorithm is O(log7 n).
Space complexity
We store one character per base-7 digit, so the overall the space complexity will be O(log7 n).
Common pitfalls and interview tips
- Forgetting the zero case
Ifnum == 0, the loop never runs. Always handle this upfront. - Incorrect handling of negative numbers
Convert the number to positive first, then reapply the sign at the end. - Reversing logic mistakes
Remainders are generated in reverse order. Forgetting to reverse is a common bug. - Using built-in base conversion directly
Interviewers expect you to demonstrate the logic, not rely on language shortcuts.