PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to represent and iterate IPv4 addresses, covering parsing, numeric arithmetic with carry between octets, and the design of a stateful iterator.

  • medium
  • Anthropic
  • Coding & Algorithms
  • Software Engineer

Design an IPv4 Address Iterator

Company: Anthropic

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Design a class that iterates through IPv4 addresses. An IPv4 address is a string in the form `a.b.c.d`, where each octet is an integer from `0` to `255`. Implement an iterator with the following behavior: - The constructor receives: - `start_ip`: the first IPv4 address in the sequence - `end_ip`: the last IPv4 address that may be returned - `step`: a positive integer increment - The increment is fixed for the lifetime of the object and is provided in the constructor, not passed into `next()`. - `next()` returns the current IP address and then advances by `step` addresses. - `has_next()` returns whether another valid address can still be returned without passing `end_ip`. You should correctly handle carry between octets. For example: - incrementing `10.0.0.255` by 1 gives `10.0.1.0` - incrementing `192.168.1.254` by 2 gives `192.168.2.0` Assume all input IP strings are valid and `start_ip <= end_ip`. Example: - `start_ip = "192.168.0.254"` - `end_ip = "192.168.1.4"` - `step = 2` The iterator should return: - `192.168.0.254` - `192.168.1.0` - `192.168.1.2` - `192.168.1.4` Follow-up: What internal representation would you choose to simplify the implementation and avoid repeated string parsing?

Quick Answer: This question evaluates a candidate's ability to represent and iterate IPv4 addresses, covering parsing, numeric arithmetic with carry between octets, and the design of a stateful iterator.

Design the logic for an IPv4 address iterator. An IPv4 address is a string in the form `a.b.c.d`, where each octet is an integer from `0` to `255`. A conceptual iterator is created with: - `start_ip`: the first IPv4 address in the sequence - `end_ip`: the last IPv4 address that may be returned - `step`: a positive integer increment The iterator behaves like this: - `next()` returns the current IP address and then advances by `step` addresses - `has_next()` returns whether another valid address can still be returned without passing `end_ip` - The iterator includes `end_ip` only if it is landed on exactly For this coding problem, instead of implementing a class, write a function that returns the full list of IPv4 addresses that would be produced by repeated calls to `next()` while `has_next()` is true. You must correctly handle carry between octets. For example: - incrementing `10.0.0.255` by 1 gives `10.0.1.0` - incrementing `192.168.1.254` by 2 gives `192.168.2.0` Follow-up to consider: what internal representation makes the implementation simpler and avoids repeated string parsing?

Constraints

  • All input IP strings are valid IPv4 addresses with octets in the range 0 to 255.
  • `start_ip` is less than or equal to `end_ip` in numeric IPv4 order.
  • `step` is a positive integer.
  • The number of returned addresses will not exceed 10^5.

Examples

Input: ('192.168.0.254', '192.168.1.4', 2)

Expected Output: ['192.168.0.254', '192.168.1.0', '192.168.1.2', '192.168.1.4']

Explanation: Starting at 192.168.0.254 and adding 2 each time gives 192.168.1.0, then 192.168.1.2, then 192.168.1.4.

Input: ('10.0.0.255', '10.0.1.1', 1)

Expected Output: ['10.0.0.255', '10.0.1.0', '10.0.1.1']

Explanation: Incrementing 10.0.0.255 by 1 carries into the next octet, producing 10.0.1.0.

Input: ('0.0.0.0', '0.0.0.0', 5)

Expected Output: ['0.0.0.0']

Explanation: The start and end are the same, so the iterator yields exactly one address.

Input: ('1.2.3.4', '1.2.3.10', 20)

Expected Output: ['1.2.3.4']

Explanation: After returning the start address, advancing by 20 would go past the end, so no more addresses are produced.

Input: ('0.255.255.255', '1.0.0.3', 2)

Expected Output: ['0.255.255.255', '1.0.0.1', '1.0.0.3']

Explanation: Adding 2 to 0.255.255.255 carries across multiple octets and produces 1.0.0.1.

Hints

  1. Think of an IPv4 address as a single 32-bit integer, with each octet representing one byte.
  2. If you convert `start_ip` and `end_ip` once at the beginning, advancing by `step` becomes simple integer addition.
Last updated: Apr 28, 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
  • 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

  • Implement a Banking System - Anthropic (medium)
  • Implement Persistent Memoization LRU Cache - Anthropic (hard)
  • Fix a Corrupted Bootloader Instruction - Anthropic (medium)
  • Implement a Time-Aware Task Manager - Anthropic (hard)
  • Implement a Simplified DNS Resolver - Anthropic (hard)