Hexadecimal and Hexatrigesimal Conversion
What is this problem about?
The Hexadecimal and Hexatrigesimal Conversion interview question involves converting numbers between different bases. Hexadecimal is base-16 (digits 0-9, A-F), while Hexatrigesimal is base-36 (digits 0-9, A-Z). You may be asked to convert a decimal number to one of these bases or convert a string from one base directly to another.
Why is this asked in interviews?
Base conversion is a fundamental computer science concept. It tests a candidate's understanding of positional notation and modular arithmetic. Companies like Zopsmart use this to ensure developers can handle data representation tasks, which are common in low-level programming, URL shortening services (base-36/base-62), and data encoding.
Algorithmic pattern used
This follows the standard Math interview pattern for base conversion:
- Decimal to Base N: Repeatedly take the number modulo N to find the current digit, then divide by N. Map the remainder to the appropriate character (0-9, A-Z).
- Base N to Decimal: Iterate through the string from right to left (or left to right with power tracking). Multiply each digit's value by Nposition and sum them up.
Example explanation
Convert 1234 to Base-36:
- 1234÷36=34 remainder 10. Remainder 10 in base-36 is 'A'.
- 34÷36=0 remainder 34. Remainder 34 in base-36 is 'Y'.
Result:
"YA".
Common mistakes candidates make
- Character Mapping: Forgetting that 'A' represents 10 and 'Z' represents 35.
- String Reversal: Forgetting that the remainders are generated from the least significant digit to the most significant, so the resulting string must be reversed.
- Overflow: Using standard 32-bit integers for base-36 conversions of very long strings, which can easily exceed the integer limit.
Interview preparation tip
Always clarify if the conversion needs to handle negative numbers or very large values (BigInt). Understanding how to convert to base-36 is particularly useful for system design questions involving unique ID generation or URL shorteners.