Level Up Your Coding Skills & Crack Interviews — Save up to 50% or more on Educative.io Today! Claim Discount

Arrow
Table of contents

504. Base 7

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.

Base 7 is a positional numeral system that uses digits 0 through 6. The conversion must follow standard base conversion rules and should correctly handle negative numbers.

Constraints:

  • −107 <= num <= 107

Examples

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

A number is constructed by repeatedly dividing by the base and collecting remainders. In base 10, we are accustomed to thinking in powers of 10. Whereas, in base 7, the idea is identical the only difference is that each digit represents a power of 7 instead.

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

  1. If num == 0, return "0" immediately.
  2. Record whether the number is negative.
  3. Convert num to its absolute value.
  4. Initialize an empty list digits to store digits.
  5. While num > 0:
    1. Append num % 7 to the list.
    2. Update num = num // 7.
  6. Reverse the digit list and join it into a string.
  7. Add a '-' sign if the original number was negative.

Let’s look at the following illustration to get a better understanding of the solution:

1 / 4

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

  1. Forgetting the zero case
    If num == 0, the loop never runs. Always handle this upfront.
  2. Incorrect handling of negative numbers
    Convert the number to positive first, then reapply the sign at the end.
  3. Reversing logic mistakes
    Remainders are generated in reverse order. Forgetting to reverse is a common bug.
  4. Using built-in base conversion directly
    Interviewers expect you to demonstrate the logic, not rely on language shortcuts.

Interview tip

When explaining your solution, emphasize why modulo gives the least-significant digit and why division shifts the number. This shows real understanding of positional numeral systems.

Frequently Asked Questions

Can this be generalized to any base?

Yes. Replace 7 with any base k and adjust the digit mapping if k > 10.

Why do we reverse the digits at the end?

Because remainders are produced from least significant digit to most significant digit.

Is recursion a good alternative?

It works conceptually, but iterative solutions are clearer and avoid recursion overhead.

Share with others:

Leave a Reply

Your email address will not be published. Required fields are marked *