LeetCode Problem Workspace
Base 7
Convert any given integer to its base 7 string representation using efficient math and string manipulation techniques.
2
Topics
6
Code langs
3
Related
Practice Focus
Easy · Math plus String
Answer-first summary
Convert any given integer to its base 7 string representation using efficient math and string manipulation techniques.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Math plus String
To solve Base 7, immediately apply repeated division and modulo operations to extract digits in base 7. Concatenate these digits carefully into a string, handling negative numbers with a leading minus sign. This approach ensures both correctness and efficiency for any integer within constraints.
Problem Statement
Given an integer num, return its string representation in base 7. Negative numbers should start with a '-' sign. Implement an efficient method combining arithmetic and string operations.
For example, if num = 100, the output should be "202" because 100 in base 7 is 2 7^2 + 0 7^1 + 2*7^0. Similarly, num = -7 should return "-10".
Examples
Example 1
Input: num = 100
Output: "202"
Example details omitted.
Example 2
Input: num = -7
Output: "-10"
Example details omitted.
Constraints
- -107 <= num <= 107
Solution Approach
Use repeated division and modulo
Divide the number by 7 repeatedly, storing the remainder each time. Build the base 7 digits in reverse order, then join them into a final string. This ensures correct digit order for the base 7 representation.
Handle negative numbers carefully
If the input number is negative, compute the base 7 string for its absolute value, then prepend a '-' sign. This avoids sign errors that commonly appear in naive implementations.
Convert remainders to string efficiently
Each remainder from the division corresponds to a single base 7 digit. Convert integers to string using standard methods and concatenate them efficiently to minimize string-building overhead.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(log7|num|) because each division reduces the number by a factor of 7. Space complexity is also O(log7|num|) to store the resulting digits in a string.
What Interviewers Usually Probe
- Are you handling negative numbers correctly with a leading '-' sign?
- Can you perform the conversion without using built-in base functions?
- Are you efficiently building the string from the remainders to avoid order issues?
Common Pitfalls or Variants
Common pitfalls
- Forgetting to prepend '-' for negative numbers can cause incorrect output.
- Reversing the digits incorrectly when joining remainders results in wrong base 7 string.
- Using float division instead of integer division may produce unexpected results.
Follow-up variants
- Convert an integer to any arbitrary base k as a string.
- Handle very large integers efficiently using string concatenation only.
- Convert a list of integers to their base 7 strings in batch processing.
FAQ
What is the Base 7 problem about?
It requires converting a given integer into its base 7 string representation, handling both positive and negative numbers.
Can I use built-in functions to solve Base 7?
While some languages offer base conversion utilities, interview best practice is implementing repeated division and modulo manually.
How do negative numbers affect Base 7 conversion?
Compute the conversion on the absolute value, then prepend a '-' to ensure correct output.
What is the time complexity for Base 7 conversion?
The time complexity is O(log7|num|) because each division reduces the number by a factor of 7.
Does Base 7 conversion tie to a common interview pattern?
Yes, it exemplifies the 'Math plus String' pattern, combining arithmetic digit extraction with careful string construction.
Solution
Solution 1
#### Python3
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])Continue Topic
math
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Math plus String
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward