PracHub
QuestionsCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/OpenAI

Implement IPv4 and CIDR iterators

Last updated: Apr 28, 2026

Quick Overview

This question evaluates understanding of IPv4 addressing and CIDR notation, along with competency in binary/bitwise arithmetic, iterator construction, and edge-case handling for address boundaries.

  • medium
  • OpenAI
  • Coding & Algorithms
  • Software Engineer

Implement IPv4 and CIDR iterators

Company: OpenAI

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem: IPv4 / CIDR Iterator You are building utilities to iterate through IPv4 addresses. ### Definitions - An IPv4 address is in dotted decimal form: `a.b.c.d`, where each octet is `0..255`. - A CIDR block is `ip/prefixLen`, e.g. `192.168.1.0/24`, where `prefixLen` is `0..32`. ### Tasks Implement functions/classes to support: 1. **Ascending iteration of a single IPv4 address** - Given an IPv4 address string, return the **next** IPv4 address (i.e., add 1 to the 32-bit value with carry across octets). - Define behavior when the input is `255.255.255.255` (e.g., error, wrap, or return null) and be consistent. 2. **Descending iteration of a single IPv4 address** - Given an IPv4 address string, return the **previous** IPv4 address (subtract 1 from the 32-bit value with borrow across octets). - Define behavior when the input is `0.0.0.0`. 3. **Iteration over a CIDR block** - Given a CIDR string like `10.0.0.0/30`, produce an iterator (or list/stream) of all IPv4 addresses in that block. - Specify ordering (ascending is typical). - Clarify whether you include the network and broadcast addresses; implement accordingly. ### Examples - `next("1.2.3.4") -> "1.2.3.5"` - `next("1.2.3.255") -> "1.2.4.0"` - `prev("1.0.0.0") -> "0.255.255.255"` - For `"192.168.1.0/30"`, the block size is 4 addresses; iterate all addresses in that range. ### Constraints / Notes - Assume valid input formatting unless you choose to add validation. - Aim for O(1) work per `next()`/`prev()` step and O(1) extra space (besides the output).

Quick Answer: This question evaluates understanding of IPv4 addressing and CIDR notation, along with competency in binary/bitwise arithmetic, iterator construction, and edge-case handling for address boundaries.

Related Interview Questions

  • Consistent Hashing Ring with Virtual Nodes for Shard Rebalancing - OpenAI (hard)
  • Infection Spread Simulation with Death Threshold - OpenAI (medium)
  • Spreading Contagion on a Grid - OpenAI (medium)
  • Streaming Entropy with Numerical Stability - OpenAI (hard)
  • Implement a Distributed Rate Limiter - OpenAI (medium)
|Home/Coding & Algorithms/OpenAI

Implement IPv4 and CIDR iterators

OpenAI logo
OpenAI
Jan 10, 2025, 12:00 AM
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
11
0
Practice Read
Loading...

Problem: IPv4 / CIDR Iterator

You are building utilities to iterate through IPv4 addresses.

Definitions

  • An IPv4 address is in dotted decimal form: a.b.c.d , where each octet is 0..255 .
  • A CIDR block is ip/prefixLen , e.g. 192.168.1.0/24 , where prefixLen is 0..32 .

Tasks

Implement functions/classes to support:

  1. Ascending iteration of a single IPv4 address
    • Given an IPv4 address string, return the next IPv4 address (i.e., add 1 to the 32-bit value with carry across octets).
    • Define behavior when the input is 255.255.255.255 (e.g., error, wrap, or return null) and be consistent.
  2. Descending iteration of a single IPv4 address
    • Given an IPv4 address string, return the previous IPv4 address (subtract 1 from the 32-bit value with borrow across octets).
    • Define behavior when the input is 0.0.0.0 .
  3. Iteration over a CIDR block
    • Given a CIDR string like 10.0.0.0/30 , produce an iterator (or list/stream) of all IPv4 addresses in that block.
    • Specify ordering (ascending is typical).
    • Clarify whether you include the network and broadcast addresses; implement accordingly.

Examples

  • next("1.2.3.4") -> "1.2.3.5"
  • next("1.2.3.255") -> "1.2.4.0"
  • prev("1.0.0.0") -> "0.255.255.255"
  • For "192.168.1.0/30" , the block size is 4 addresses; iterate all addresses in that range.

Constraints / Notes

  • Assume valid input formatting unless you choose to add validation.
  • Aim for O(1) work per next() / prev() step and O(1) extra space (besides the output).

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More OpenAI•More Software Engineer•OpenAI Software Engineer•OpenAI Coding & Algorithms•Software Engineer Coding & Algorithms
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.