KatInterview

504. Base 7

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to convert an integer from its base-10 representation to a base-7 representation and return it as a string. The input integer can be positive, negative, or zero.

Solution

Base 7 in Python

class Solution:
    def convertToBase7(self, num: int) -> str:
        # Handle the edge case of 0.
        if num == 0:
            return "0"
        
        # Note if the number is negative and work with its absolute value.
        is_negative = num < 0
        n = abs(num)
        
        res = ""
        # Repeatedly take the remainder and divide by 7.
        # Prepending the digit avoids a final reverse step.
        while n > 0:
            res = str(n % 7) + res
            n //= 7
            
        # If the original number was negative, prepend the negative sign.
        if is_negative:
            return "-" + res
        else:
            return res

Base 7 in Java

class Solution {
    public String convertToBase7(int num) {
        // Handle the edge case of 0.
        if (num == 0) {
            return "0";
        }

        // Note if the number is negative and work with its absolute value.
        boolean isNegative = num < 0;
        int n = Math.abs(num);

        // StringBuilder is efficient for string concatenations in a loop.
        StringBuilder sb = new StringBuilder();
        
        // Repeatedly take the remainder of division by 7 and then divide the number by 7.
        while (n > 0) {
            sb.append(n % 7);
            n /= 7;
        }

        // If the original number was negative, append the sign. 
        // It will be at the end before reversing.
        if (isNegative) {
            sb.append('-');
        }

        // The digits are generated in reverse order. Reverse the string to get the correct representation.
        // The negative sign, if appended, will now be at the front.
        return sb.reverse().toString();
    }
}

Base 7 in C++

class Solution {
public:
    string convertToBase7(int num) {
        // Handle the edge case of 0.
        if (num == 0) {
            return "0";
        }

        // Note if the number is negative and work with its absolute value.
        bool isNegative = num < 0;
        int n = abs(num);
        
        string res = "";
        // Repeatedly take the remainder of division by 7 and then divide the number by 7.
        while (n > 0) {
            res += to_string(n % 7);
            n /= 7;
        }
        
        // The digits are generated in reverse order, so we need to reverse the string.
        reverse(res.begin(), res.end());
        
        // If the original number was negative, prepend the negative sign.
        if (isNegative) {
            return "-" + res;
        }
        
        return res;
    }
};

Base 7 in TypeScript

function convertToBase7(num: number): string {
    // Handle the edge case of 0.
    if (num === 0) {
        return "0";
    }

    // Note if the number is negative and work with its absolute value.
    const isNegative = num < 0;
    let n = Math.abs(num);

    let res = '';
    // Repeatedly take the remainder and divide by 7.
    // Prepending the digit avoids a final reverse step.
    while (n > 0) {
        res = (n % 7) + res;
        n = Math.floor(n / 7);
    }

    // If the original number was negative, prepend the negative sign.
    return isNegative ? '-' + res : res;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.