PracHub
QuestionsPremiumLearningGuidesCheatsheetNEW
|Home/Coding & Algorithms/Anthropic

Design an IPv4 Address Iterator

Last updated: Apr 28, 2026

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.

Related Interview Questions

  • Convert Samples into Event Intervals - Anthropic (medium)
  • Convert State Stream to Events - Anthropic (medium)
  • Build a concurrent web crawler - Anthropic (medium)
  • Implement a Parallel Image Processor - Anthropic (medium)
  • Implement a Batch Image Processor - Anthropic (medium)
Anthropic logo
Anthropic
Jan 10, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
12
0
Coding Console
Loading...

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?

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

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