Quick Overview

This question evaluates understanding of integer representation, hexadecimal notation, bitwise operations, and 32-bit two's complement arithmetic. It is commonly asked to assess low-level data representation and edge-case handling within the Coding & Algorithms domain, and it tests practical implementation skills alongside conceptual understanding of binary and base conversion.

Convert 32-bit integer to hexadecimal

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Problem Given a 32-bit signed integer `num`, return its **hexadecimal representation** as a string. ## Requirements - Use **lowercase** letters `a-f`. - Do **not** include leading zeros (unless the value is exactly zero). - For **negative** numbers, return the hexadecimal representation of the corresponding **32-bit two's complement** value. ## Input - Integer `num` in range `[-2^31, 2^31 - 1]` ## Output - A string containing the hexadecimal representation. ## Examples - `num = 26` → `"1a"` - `num = 0` → `"0"` - `num = -1` → `"ffffffff"` ## Follow-ups (optional) - Implement without using built-in base conversion utilities (e.g., no `toString(16)` / `format`).

Quick Answer: This question evaluates understanding of integer representation, hexadecimal notation, bitwise operations, and 32-bit two's complement arithmetic. It is commonly asked to assess low-level data representation and edge-case handling within the Coding & Algorithms domain, and it tests practical implementation skills alongside conceptual understanding of binary and base conversion.

## Problem Given a 32-bit signed integer `num`, return its **hexadecimal representation** as a string. ## Requirements - Use **lowercase** letters `a-f`. - Do **not** include leading zeros (unless the value is exactly zero, which returns `"0"`). - For **negative** numbers, return the hexadecimal representation of the corresponding **32-bit two's complement** value. ## Input - Integer `num` in range `[-2^31, 2^31 - 1]` ## Output - A string containing the lowercase hexadecimal representation. ## Examples - `num = 26` -> `"1a"` - `num = 0` -> `"0"` - `num = -1` -> `"ffffffff"` ## Follow-up - Implement without using built-in base-conversion utilities (no `toString(16)` / `format` / `Integer.toHexString`).

Constraints

  • -2^31 <= num <= 2^31 - 1
  • Output uses lowercase letters a-f.
  • No leading zeros, except num = 0 which returns "0".
  • Negative numbers use 32-bit two's complement representation.

Examples

Input: (26,)

Expected Output: "1a"

Explanation: 26 = 0x1a. Two nibbles: 26 & 0xf = 10 -> 'a', then 26 >> 4 = 1 -> '1'. Reversed gives "1a".

Input: (0,)

Expected Output: "0"

Explanation: Zero is the special case and returns "0" directly (no leading-zero stripping would otherwise leave an empty string).

Hints

  1. Handle num == 0 as a special case up front, otherwise the loop produces an empty string.
  2. For negative numbers, add 2^32 (1 << 32) to recover the unsigned 32-bit two's complement value, then convert that.
  3. Process 4 bits (one hex nibble) at a time: take num & 0xf to index into "0123456789abcdef", then right-shift by 4.
  4. Build the digits from least-significant to most-significant, then reverse the result before returning.

Loading coding console...