Level: Senior-Level
Round: Phone Screen · Type: Coding · Difficulty: 5/10 · Duration: 60 min · Interviewer: Neutral
Topics: Iterator, IPv4 Address
Location: San Francisco Bay Area
Interview date: 2026-01-20
I had to implement an IPv4 iterator with hasNext() and next() methods.
The coding question I got was:
` class IPv4Iterator: def init(self, startIp): # Initializes the iterator with the startIp. # The first address yielded by the iterator must be exactly startIp. pass
def hasNext(self):
# Returns true if there is at least one address left to be produced.
# Otherwise, returns false.
pass
def next(self):
# Returns the current address as a string in dotted-decimal form,
# then advances the internal cursor to the next sequential address.
# It is guaranteed that next() is called only when hasNext() returns true.
# The iterator visits addresses in strict ascending order (e.g., after "x.y.z.255" comes "x.y.(z+1).0").
pass
`
Constraints:
startIp is a valid IPv4 address string.0 ≤ startIp ≤ 255.255.255.255.Example:
` Input: ["IPv4Iterator", "next", "next", "next", "hasNext"] [["255.255.255.253"], [], [], [], []]
Output: [null, "255.255.255.253", "255.255.255.254", "255.255.255.255", false]
Explanation:
IPv4Iterator iterator = new IPv4Iterator("255.255.255.253"); iterator.next(); // Returns "255.255.255.253". iterator.next(); // Returns "255.255.255.254". iterator.next(); // Returns "255.255.255.255". iterator.hasNext(); // Returns false. `