PracHub
QuestionsCoachesLearningGuidesInterview Prep

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.

  • medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

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).

Input: (-1,)

Expected Output: "ffffffff"

Explanation: -1 in 32-bit two's complement is 0xFFFFFFFF. Adding 1<<32 to -1 yields 4294967295 = ffffffff.

Input: (255,)

Expected Output: "ff"

Explanation: 255 = 0xff, exactly one byte: two 'f' nibbles.

Input: (16,)

Expected Output: "10"

Explanation: 16 = 0x10. 16 & 0xf = 0 -> '0', 16 >> 4 = 1 -> '1'. Reversed gives "10".

Input: (2147483647,)

Expected Output: "7fffffff"

Explanation: INT_MAX = 2^31 - 1 = 0x7FFFFFFF.

Input: (-2147483648,)

Expected Output: "80000000"

Explanation: INT_MIN = -2^31. Adding 1<<32 gives 2147483648 = 0x80000000.

Input: (1,)

Expected Output: "1"

Explanation: Single nibble: 1 & 0xf = 1 -> '1', then 1 >> 4 = 0 ends the loop.

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.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)