Skip to main content

Base 7 - Video Solutions

EasyMath

Base 7 - Leetcode 504 - Bit Manipulation (Python)

Greg Hogg
5:205,277 views
10 video solutions available

Base 7 - Video Solution

Watch 10 video solutions for Base 7, a easy level problem involving Math. This walkthrough by Greg Hogg has 5,277 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

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 <= 107
Read full problem with examples

Approach Overview

Problem 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.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative DivisionO(log₇ n)O(log₇ n)General case; simplest and most interview‑friendly implementation
Recursive DivisionO(log₇ n)O(log₇ n)When demonstrating recursion patterns or recursive number decomposition