PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches

Quick Overview

This question evaluates proficiency in IP addressing, parsing and validation, binary/address-space arithmetic, CIDR subnet calculation, and formatting for both IPv4 and IPv6, testing bit-level manipulation and string-processing skills in the Networking and Coding & Algorithms domain.

  • hard
  • OpenAI
  • Coding & Algorithms
  • Software Engineer

Implement IP Address Arithmetic

Company: OpenAI

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

Implement an IP address utility library. Support the following operations: 1. Parse an IPv4 address in dotted-decimal form, such as `192.168.0.1`. 2. Given an IPv4 address and a direction, return the adjacent address: - `up` means add one. - `down` means subtract one. - If the result would underflow below `0.0.0.0` or overflow above `255.255.255.255`, return an error. 3. Parse an IPv4 CIDR block such as `192.168.0.0/24` and compute the first and last addresses in the block. 4. Determine whether an IPv4 address belongs to a given IPv4 CIDR block. 5. Extend the same adjacent-address operation to IPv6 addresses represented as eight 16-bit hexadecimal groups separated by periods, for example `ffff.ffff.ffff.ffff.ffff.ffff.ffff.fffa`. Design clean parsing, validation, conversion, and formatting logic. Pay attention to boundary cases.

Quick Answer: This question evaluates proficiency in IP addressing, parsing and validation, binary/address-space arithmetic, CIDR subnet calculation, and formatting for both IPv4 and IPv6, testing bit-level manipulation and string-processing skills in the Networking and Coding & Algorithms domain.

Part 1: Validate and Parse an IPv4 Address

Given a string ip_address, validate it as a dotted-decimal IPv4 address. A valid IPv4 address has exactly four dot-separated parts, each made only of decimal digits and representing a value from 0 to 255. Leading zeros are allowed and should be parsed normally. Return the four octets as a list of integers. If the address is invalid, return an empty list.

Constraints

  • 1 <= len(ip_address) <= 100
  • The address must contain exactly 4 parts separated by '.'
  • Each part must contain only decimal digits and represent a value in [0, 255]
  • Do not use external networking libraries

Examples

Input: "192.168.0.1"

Expected Output: [192, 168, 0, 1]

Explanation: A standard valid IPv4 address.

Input: "0.0.0.0"

Expected Output: [0, 0, 0, 0]

Explanation: Lower boundary address.

Input: "255.255.255.255"

Expected Output: [255, 255, 255, 255]

Explanation: Upper boundary address.

Input: "10.01.2.003"

Expected Output: [10, 1, 2, 3]

Explanation: Leading zeros are allowed and parsed as decimal values.

Input: "256.1.2.3"

Expected Output: []

Explanation: 256 is outside the valid octet range.

Hints

  1. Split the string on '.' and first check whether you got exactly four pieces.
  2. Validate each piece before converting it: it should be non-empty, all digits, and within the range 0 to 255.

Part 2: Find the Adjacent IPv4 Address

Given an IPv4 address in dotted-decimal form and a direction string, return the adjacent IPv4 address. If direction is 'up', add one. If direction is 'down', subtract one. If the input address is invalid, the direction is invalid, or the result would underflow below 0.0.0.0 or overflow above 255.255.255.255, return an empty string. Output must be in canonical dotted-decimal form with no leading zeros.

Constraints

  • 1 <= len(ip_address) <= 100
  • direction is expected to be 'up' or 'down'
  • Use only manual parsing and arithmetic; do not use networking libraries

Examples

Input: ("192.168.0.1", "up")

Expected Output: "192.168.0.2"

Explanation: Simple increment within the same last octet.

Input: ("192.168.0.255", "up")

Expected Output: "192.168.1.0"

Explanation: Increment carries into the next octet.

Input: ("10.0.0.0", "down")

Expected Output: "9.255.255.255"

Explanation: Decrement borrows across octet boundaries.

Input: ("0.0.0.0", "down")

Expected Output: ""

Explanation: This would underflow below the smallest IPv4 address.

Input: ("255.255.255.255", "up")

Expected Output: ""

Explanation: This would overflow above the largest IPv4 address.

Hints

  1. Convert the 4 octets into one 32-bit integer, then the adjacent address is just plus or minus 1.
  2. Check the boundary values before changing the number: 0 for underflow and 2^32 - 1 for overflow.

Part 3: Compute the First and Last IPv4 Addresses in a CIDR Block

Given an IPv4 CIDR block in the form A.B.C.D/P, compute the inclusive first and last IPv4 addresses in that block. The A.B.C.D part does not have to already be a network address; apply the prefix mask first. Return [first_address, last_address]. If the CIDR string is invalid, return an empty list.

Constraints

  • 1 <= len(cidr) <= 100
  • The prefix length P must be an integer in [0, 32]
  • The IPv4 portion must be a valid dotted-decimal IPv4 address
  • Do not use external networking libraries

Examples

Input: "192.168.0.0/24"

Expected Output: ["192.168.0.0", "192.168.0.255"]

Explanation: A standard /24 block.

Input: "192.168.5.123/24"

Expected Output: ["192.168.5.0", "192.168.5.255"]

Explanation: Host bits in the given base address are ignored when computing the block range.

Input: "10.0.0.1/32"

Expected Output: ["10.0.0.1", "10.0.0.1"]

Explanation: A /32 block contains exactly one address.

Input: "0.0.0.0/0"

Expected Output: ["0.0.0.0", "255.255.255.255"]

Explanation: A /0 block covers the entire IPv4 address space.

Input: "1.2.3.4/33"

Expected Output: []

Explanation: 33 is not a valid IPv4 prefix length.

Hints

  1. Build the subnet mask from the prefix length: the top P bits are 1 and the remaining bits are 0.
  2. The first address is ip & mask, and the last address is first | (~mask within 32 bits).

Part 4: Check Whether an IPv4 Address Belongs to a CIDR Block

Given an IPv4 address and an IPv4 CIDR block, determine whether the address belongs to the block. Return 1 if it belongs, 0 if it does not, and -1 if either input is invalid. Membership is inclusive, so the first and last addresses of the block count as inside.

Constraints

  • 1 <= len(ip_address), len(cidr) <= 100
  • The CIDR prefix length must be in [0, 32]
  • Use only manual parsing and arithmetic; do not use networking libraries

Examples

Input: ("192.168.0.10", "192.168.0.0/24")

Expected Output: 1

Explanation: The address falls inside the /24 range.

Input: ("192.168.0.200", "192.168.0.99/24")

Expected Output: 1

Explanation: The block is still 192.168.0.0/24 after masking the base address.

Input: ("192.168.1.10", "192.168.0.0/24")

Expected Output: 0

Explanation: The third octet differs from the target /24 block.

Input: ("10.0.0.0", "10.0.0.0/32")

Expected Output: 1

Explanation: A /32 block contains exactly one address, and this is it.

Input: ("1.2.3.4", "1.2.3.4/33")

Expected Output: -1

Explanation: The CIDR block is invalid because the prefix is out of range.

Hints

  1. Two addresses are in the same block if their network portions are equal after applying the same subnet mask.
  2. A /0 mask is valid and matches every valid IPv4 address.

Part 5: Find the Adjacent IPv6 Address

Given an IPv6 address written as eight 16-bit hexadecimal groups separated by periods and a direction, return the adjacent IPv6 address. If direction is 'up', add one. If direction is 'down', subtract one. Accept 1 to 4 hexadecimal digits per group, case-insensitively. The output must be normalized to exactly eight lowercase groups of four hexadecimal digits, separated by periods. If the input is invalid, the direction is invalid, or the result would underflow or overflow the 128-bit IPv6 range, return an empty string. This problem intentionally uses periods instead of colons and does not allow compressed forms like ::.

Constraints

  • 1 <= len(ipv6_address) <= 200
  • The IPv6 address must contain exactly 8 groups separated by '.'
  • Each group must contain 1 to 4 hexadecimal characters
  • Compressed notation such as '::' is not allowed
  • Do not use external networking libraries

Examples

Input: ("ffff.ffff.ffff.ffff.ffff.ffff.ffff.fffa", "up")

Expected Output: "ffff.ffff.ffff.ffff.ffff.ffff.ffff.fffb"

Explanation: A simple increment in the last group.

Input: ("0000.0000.0000.0000.0000.0000.0000.ffff", "up")

Expected Output: "0000.0000.0000.0000.0000.0000.0001.0000"

Explanation: Increment causes a carry into the previous 16-bit group.

Input: ("ABCD.0.0.0.0.0.0.1", "down")

Expected Output: "abcd.0000.0000.0000.0000.0000.0000.0000"

Explanation: Input is case-insensitive and output is normalized to lowercase four-digit groups.

Input: ("0000.0000.0000.0000.0000.0000.0000.0000", "down")

Expected Output: ""

Explanation: This would underflow below the smallest IPv6 address.

Input: ("ffff.ffff.ffff.ffff.ffff.ffff.ffff.ffff", "up")

Expected Output: ""

Explanation: This would overflow above the largest IPv6 address.

Hints

  1. Treat each group as a 16-bit chunk and build one 128-bit integer by shifting left 16 bits at a time.
  2. When formatting the answer, always print each group with exactly 4 lowercase hexadecimal digits.
Last updated: May 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ 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
  • 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

  • Simulate Infection Spread on a Grid - OpenAI (hard)
  • Implement Social Follow Recommendations - OpenAI (medium)
  • Build a Compose Rating Card - OpenAI (medium)
  • Generate Data Labeling Schedules - OpenAI (medium)
  • Build a Streaming Chat Input - OpenAI (medium)