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 Overview: Given an integer num, return its representation in base 7 as a string. The task is essentially base conversion: repeatedly divide the number by 7 and collect the remainders that form the digits of the base‑7 number.
Approach 1: Iterative Division Approach (O(log7 n) time, O(log7 n) space)
The standard base conversion technique repeatedly divides the number by the target base and records the remainder. Start with num, compute num % 7 to get the least significant base‑7 digit, then update num = num // 7. Continue until the number becomes zero. Because digits are produced from least significant to most significant, append them to a buffer and reverse at the end (or prepend each digit). Handle negative numbers by storing the sign and converting the absolute value first. This approach runs in O(log7 n) time because each division reduces the number by a factor of 7, and it uses O(log7 n) space for the resulting string. The logic relies purely on arithmetic operations from math, making it simple and efficient.
Approach 2: Recursive Division Approach (O(log7 n) time, O(log7 n) space)
The same division idea can be expressed recursively. Instead of building digits iteratively, recursively process num // 7 until the base case (num < 7) is reached. Each recursive call returns the base‑7 representation of the higher digits, and the current remainder num % 7 is appended to the result. The recursion depth equals the number of base‑7 digits, which is O(log7 n). Space complexity is also O(log7 n) due to the call stack. This style highlights the natural decomposition of the number into higher digits and the current digit, a common pattern in recursion problems involving numeric representation.
Both approaches rely on the same mathematical insight: any number in base 10 can be represented as repeated divisions by the new base, collecting remainders as digits. The iterative version builds the result explicitly with a loop, while the recursive version constructs it during the return phase of function calls.
Recommended for interviews: The iterative division approach is typically expected. It demonstrates that you understand base conversion mechanics and can implement it efficiently using simple arithmetic operations. The recursive approach is clean and expressive but adds call‑stack overhead, so it’s better presented as an alternative after explaining the iterative method. Showing both reinforces your understanding of number representation and basic math transformations.
This approach involves converting the number to base 7 by repeatedly dividing the number by 7 and noting the remainders. This is similar to how you would convert a decimal number to any other base. We gather the remainders which will form the digits of the number in base 7, with the first remainder being the least significant digit.
We first handle the case for the number being 0. For non-zero numbers, we compute the absolute value, repeatedly divide by 7 to obtain remainders, and store them as characters in a buffer. If the number was negative, we append a '-' after collecting all digits. Finally, the digits are reversed to correct order before returning the string.
Time Complexity: O(log7(n)), where n is the absolute value of the input number. This is due to the division operations required to reduce the number to zero.
Space Complexity: O(log7(n)), due to the storage required for the result string in base 7.
This recursive approach streamlines handling the conversion by leveraging function calls to manage the digit placement. Rather than manually reversing the digits collected as in the iterative method, recursion inherently processes the most significant digit later, accommodating natural string assembly order.
The function helper is defined to perform the recursive conversions. It divides the number by 7, recursively processing all digits. The base case handles digits less than 7 directly. Negative numbers are managed by post-fixing a negative sign if the original number is negative.
Python
Time Complexity: O(log7(n))
Space Complexity: O(log7(n)), due to recursion depth.
| Approach | Complexity |
|---|---|
| Iterative Division Approach | Time Complexity: O(log7(n)), where n is the absolute value of the input number. This is due to the division operations required to reduce the number to zero. |
| Recursive Division Approach | Time Complexity: O(log7(n)) |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Division | O(log₇ n) | O(log₇ n) | General case; simplest and most interview‑friendly implementation |
| Recursive Division | O(log₇ n) | O(log₇ n) | When demonstrating recursion patterns or recursive number decomposition |
Base 7 - Leetcode 504 - Bit Manipulation (Python) • Greg Hogg • 4,946 views views
Watch 9 more video solutions →Practice Base 7 with our built-in code editor and test cases.
Practice on FleetCode