Convert a Hexadecimal Address to Dotted IPv4

Read the full interview experience this question came from →

Quick Overview

Convert an eight-digit hexadecimal address to network-order dotted IPv4 with strict validation, optional prefixes, and correct high-bit handling.

Convert a Hexadecimal Address to Dotted IPv4

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Convert a 32-bit hexadecimal address to dotted-decimal IPv4 notation. Implement `hex_to_ipv4(text: string) -> string`. ### Constraints & Assumptions The source reports a hex-to-IPv4 conversion without exact formatting rules. This practice contract uses network-order bytes: the leftmost byte is the first decimal octet. - Input length is at most 100. Accept exactly eight hexadecimal digits, optionally preceded by `0x` or `0X`. - Hexadecimal digits are case-insensitive. Whitespace, signs, separators, and any other length or character are invalid. - On valid input return four decimal octets separated by dots, without leading zeroes. On invalid input return `INVALID`. - Treat the value as an unsigned 32-bit bit pattern; addresses whose high bit is set are valid. ### Examples ```text hex_to_ipv4("C0A80101") -> "192.168.1.1" hex_to_ipv4("0xffffffff") -> "255.255.255.255" hex_to_ipv4("00000000") -> "0.0.0.0" hex_to_ipv4("1FF") -> "INVALID" ``` Explain how your byte-order choice and signed-integer handling affect the result. A solution that parses four two-character byte values can avoid signed 32-bit overflow entirely.

Overview: Convert an eight-digit hexadecimal address to network-order dotted IPv4 with strict validation, optional prefixes, and correct high-bit handling.

Read the full Microsoft Software Engineer interview experience this question came from

|Home/Coding & Algorithms/Microsoft
Microsoft logo
Microsoft
Aug 30, 2026
mediumSoftware EngineerOnsiteCoding & Algorithms
0
0

Convert a 32-bit hexadecimal address to dotted-decimal IPv4 notation.

Implement hex_to_ipv4(text: string) -> string.

Constraints & Assumptions

The source reports a hex-to-IPv4 conversion without exact formatting rules. This practice contract uses network-order bytes: the leftmost byte is the first decimal octet.

  • Input length is at most 100. Accept exactly eight hexadecimal digits, optionally preceded by 0x or 0X .
  • Hexadecimal digits are case-insensitive. Whitespace, signs, separators, and any other length or character are invalid.
  • On valid input return four decimal octets separated by dots, without leading zeroes. On invalid input return INVALID .
  • Treat the value as an unsigned 32-bit bit pattern; addresses whose high bit is set are valid.

Examples

hex_to_ipv4("C0A80101") -> "192.168.1.1"
hex_to_ipv4("0xffffffff") -> "255.255.255.255"
hex_to_ipv4("00000000") -> "0.0.0.0"
hex_to_ipv4("1FF") -> "INVALID"

Explain how your byte-order choice and signed-integer handling affect the result. A solution that parses four two-character byte values can avoid signed 32-bit overflow entirely.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...