Watch 3 video solutions for Hexadecimal and Hexatrigesimal Conversion, a easy level problem involving Math, String. This walkthrough by ADevOpsBeginner has 630 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an integer n.
Return the concatenation of the hexadecimal representation of n2 and the hexatrigesimal representation of n3.
A hexadecimal number is defined as a base-16 numeral system that uses the digits 0 – 9 and the uppercase letters A - F to represent values from 0 to 15.
A hexatrigesimal number is defined as a base-36 numeral system that uses the digits 0 – 9 and the uppercase letters A - Z to represent values from 0 to 35.
Example 1:
Input: n = 13
Output: "A91P1"
Explanation:
n2 = 13 * 13 = 169. In hexadecimal, it converts to (10 * 16) + 9 = 169, which corresponds to "A9".n3 = 13 * 13 * 13 = 2197. In hexatrigesimal, it converts to (1 * 362) + (25 * 36) + 1 = 2197, which corresponds to "1P1"."A9" + "1P1" = "A91P1".Example 2:
Input: n = 36
Output: "5101000"
Explanation:
n2 = 36 * 36 = 1296. In hexadecimal, it converts to (5 * 162) + (1 * 16) + 0 = 1296, which corresponds to "510".n3 = 36 * 36 * 36 = 46656. In hexatrigesimal, it converts to (1 * 363) + (0 * 362) + (0 * 36) + 0 = 46656, which corresponds to "1000"."510" + "1000" = "5101000".
Constraints:
1 <= n <= 1000Problem Overview: Given an integer, convert it into two different numeral systems: hexadecimal (base 16) and hexatrigesimal (base 36). The task is essentially a base conversion problem where digits beyond 9 are represented using alphabet characters. You generate the string representation for both bases.
Approach 1: Built-in Base Conversion (O(log n) time, O(log n) space)
Most languages provide built-in utilities for base conversion. For example, Python exposes hex() and formatting utilities, while Java and C++ provide methods such as Integer.toString(num, base). These functions internally perform repeated division by the base and map remainders to characters (0-9, a-z). This approach keeps the implementation short and reliable. Time complexity is O(log n) because the number of digits in base 16 or base 36 grows logarithmically with the input value. Space complexity is also O(log n) for the resulting string.
Approach 2: Simulation with Repeated Division (O(log n) time, O(log n) space)
The manual method simulates how base conversion works mathematically. Repeatedly divide the number by the target base and record the remainder. Each remainder corresponds to a digit in the new base. Values 0-9 map directly to characters, while values 10-15 (hex) or 10-35 (base36) map to letters using a lookup string such as "0123456789abcdefghijklmnopqrstuvwxyz". Append digits as you compute them, then reverse the result to produce the correct order.
This approach makes the algorithm explicit. You control digit mapping, handle edge cases like zero, and can easily adapt the logic to other bases. Each iteration reduces the number by dividing it by the base, so the loop runs roughly log_base(n) times. That yields O(log n) time complexity and O(log n) space for storing digits.
Conceptually, this is a combination of Math and String manipulation. The math part determines remainders and base reduction, while the string part handles character mapping and result construction. Many base-conversion problems follow this exact pattern, so mastering the simulation approach makes similar problems straightforward.
Recommended for interviews: The simulation approach. Interviewers expect you to understand how base conversion works internally rather than relying entirely on library helpers. Showing the repeated division process demonstrates understanding of number systems and careful string construction. Mentioning the built-in shortcut is fine, but implementing the simulation proves stronger problem-solving fundamentals.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Built-in Base Conversion | O(log n) | O(log n) | When language utilities are allowed and you want the shortest implementation |
| Simulation with Repeated Division | O(log n) | O(log n) | Preferred in interviews to demonstrate understanding of base conversion logic |