Given a 32-bit integer num, return a string representing its hexadecimal representation. For negative integers, two’s complement method is used.
All the letters in the answer string should be lowercase characters, and there should not be any leading zeros in the answer except for the zero itself.
Note: You are not allowed to use any built-in library method to directly solve this problem.
Example 1:
Input: num = 26 Output: "1a"
Example 2:
Input: num = -1 Output: "ffffffff"
Constraints:
-231 <= num <= 231 - 1When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to change a regular number into its hexadecimal form. The brute force way means trying every single possibility. We will check the number piece by piece and convert each one individually.
Here's how the algorithm would work step-by-step:
def convert_to_hexadecimal(number):
if number == 0:
return "0"
hexadecimal_characters = "0123456789abcdef"
hexadecimal_representation = ""
while number != 0:
# Get the remainder for the hexadecimal digit
remainder = number % 16
# Convert the remainder to its hexadecimal character and add to result
hexadecimal_character = hexadecimal_characters[remainder]
hexadecimal_representation = hexadecimal_character + hexadecimal_representation
# Integer divide to get the next set of digits
number = number // 16
return hexadecimal_representationThe trick is to work directly with the number's bits to build the hexadecimal representation piece by piece. We look at the number in chunks of four bits at a time and convert each chunk to its hexadecimal equivalent.
Here's how the algorithm would work step-by-step:
def convert_to_hexadecimal(number):
if number == 0:
return '0'
hexadecimal_characters = '0123456789abcdef'
result = ''
# Treat negative numbers as unsigned 32-bit integers
unsigned_number = number & 0xFFFFFFFF
while unsigned_number > 0:
# Get the last four bits of the number
four_bits = unsigned_number & 0xF
# Convert the four bits to a hexadecimal character
hexadecimal_digit = hexadecimal_characters[four_bits]
# Prepend the hexadecimal digit to the result
result = hexadecimal_digit + result
# Right shift the number by four bits
unsigned_number >>= 4
return result| Case | How to Handle |
|---|---|
| Input is 0 | Return '0' directly as the hexadecimal representation of 0 is '0'. |
| Input is the minimum integer value (Integer.MIN_VALUE) | Handles the two's complement representation correctly; ensures the hexadecimal conversion doesn't cause an integer overflow. |
| Input is a positive integer close to the maximum integer value (Integer.MAX_VALUE) | Ensures the conversion process does not result in exceeding representation bounds. |
| Input is a negative integer close to 0 (-1) | Handles the two's complement representation close to zero correctly; should return 'ffffffff'. |
| Large positive integers that, when converted to hex, have leading zeros that need to be removed. | Ensure removal of leading zeros after conversion, until a non-zero digit or a single zero remains. |
| Negative number where the most significant hex digit is 7 (e.g., 0x7FFFFFFF representing a smaller negative number) | The two's complement ensures the correct sign extension and hexadecimal representation. |
| Inputs causing bitwise operations (specifically right shifts) to result in unexpected behavior related to sign extension | Using unsigned right shift (>>> in Java) prevents sign extension issues during the conversion process. |
| Repeated calls to the function with different values, including edge cases | The function should correctly convert each independent value without side effects from previous calls. |