Magicsheet logo

Base 7

Easy
12.5%
Updated 8/1/2025

Asked by 4 Companies

Base 7

What is this problem about?

The "Base 7 interview question" is a mathematical string manipulation problem. Given an integer (which could be positive, negative, or zero), your task is to convert it into its representation in base 7. In our everyday lives, we use base 10 (decimal), but computers and specific algorithms often require switching between different numbering systems.

Why is this asked in interviews?

Top tech firms like Microsoft and Meta use the "Base 7 coding problem" to assess a candidate's understanding of number theory and basic iterative logic. It checks if the candidate can handle edge cases like the number zero and negative signs. It’s a great way to verify if someone understands how numbers are constructed fundamentally across different bases.

Algorithmic pattern used

This problem follows the Successive Division pattern, also known as the "Math interview pattern" for base conversion.

  1. Handle Zero: If the input is 0, the output is simply "0".
  2. Handle Sign: Store whether the number is negative and then work with its absolute value.
  3. Iterative Division: Repeatedly take the remainder of the number when divided by 7 (num % 7). This digit becomes part of the result.
  4. Update: Divide the number by 7 (integer division) and repeat until the number becomes 0.
  5. Reverse: The digits collected are in reverse order (from least significant to most significant), so the final string must be reversed.

Example explanation

Convert 100 to Base 7:

  1. 100÷7=14100 \div 7 = 14 remainder 2.
  2. 14÷7=214 \div 7 = 2 remainder 0.
  3. 2÷7=02 \div 7 = 0 remainder 2. Reading the remainders from bottom to top gives: 202. Result: "202".

Common mistakes candidates make

  • Forgetting the negative sign: Many candidates process the absolute value but forget to re-attach the "-" at the beginning of the string.
  • Ignoring the zero case: A simple while loop like while(n > 0) will return an empty string for the input 0, which is incorrect.
  • Wrong order: Forgetting to reverse the string or the list of remainders at the end.

Interview preparation tip

Practice base conversion for various bases (base 2, base 16, etc.). The logic remains the same: divide by the base and track the remainders. Also, get comfortable with your language's string builder or array joining methods to avoid inefficient string concatenation in loops.

Similar Questions