KatInterview

Bidirectional Walk Over a CIDR Block's Addresses

Data Structure DesignAuto-graded · 19 test cases
MediumAuto-gradedData Structure Designiteratorbit-manipulationcidr

Last updated

Simulate a two-way cursor over the IPv4 addresses of a CIDR block using integer arithmetic, with exact boundary and no-move-on-failure rules.

Walking a CIDR Block

An IPv4 CIDR block such as 192.168.1.0/24 covers a contiguous run of addresses. You are given a CIDR string and a list of cursor commands. Simulate a bidirectional cursor moving over the block's addresses in ascending numeric order, and report what each command produces.

Block definition

  • The block consists of every address from the network address (all host bits zero) to the broadcast address (all host bits one), inclusive.
  • The input address may have host bits set, for example 192.168.1.254/30. Normalize it to the network address. That block is 192.168.1.252 through 192.168.1.255.
  • Do not materialize the list of addresses. A /0 block holds 2^32 addresses.

Cursor model

Picture the cursor as sitting between addresses. It starts just before the first address of the block.

  • "next": if an address lies after the cursor, return it as a dotted-decimal string and move the cursor past it. If the cursor is already after the last address, the result is the string "StopIteration" and the cursor does not move.
  • "prev": if an address lies before the cursor, move the cursor back over it and return that address as a dotted-decimal string. If the cursor is before the first address, the result is the string "StopIteration" and the cursor does not move.

Consequently, after next, next, a prev returns the same address that the second next returned. A failed command never changes the cursor, so the walk can continue normally afterwards.

Task

Implement cidr_cursor_walk(cidr, commands, arguments). Process commands in order and return a list of strings, one per command, holding either the dotted-decimal address or "StopIteration".

arguments[i] is the argument list for commands[i]. It is always empty for these two commands and can be ignored.

If cidr is not a valid IPv4 CIDR, raise ValueError. If a command is neither "next" nor "prev", raise ValueError.

Constraints

  • cidr is an IPv4 address followed by / and a prefix length from 0 to 32.
  • commands contains only "next" and "prev".
  • len(arguments) == len(commands), and every inner list is empty.
  • Memory use must not depend on the block size.
  • Addresses are output in standard dotted-decimal form without leading zeros, for example 10.0.0.7.

Examples

Example 1

Input: cidr_cursor_walk('192.168.1.254/30', ['next', 'next', 'prev'], [[], [], []])

Output: ['192.168.1.252', '192.168.1.253', '192.168.1.253']

Example 2

Input: cidr_cursor_walk('10.0.0.0/32', ['next'], [[]])

Output: ['10.0.0.0']

Example 3

Input: cidr_cursor_walk('192.168.1.0/30', ['prev'], [[]])

Output: ['StopIteration']

Hints

Hint 1

Treat each address as a 32-bit unsigned integer. A CIDR block is then one contiguous integer interval from the network address to the broadcast address.

Hint 2

Keep a single integer cursor equal to the address that `next` would return. `next` returns it and then increments. `prev` decrements first and then returns the address it lands on.

Hint 3

Check bounds before moving. `next` fails when the cursor is past the last address, and `prev` fails when the cursor is at the first address. In both cases leave the cursor unchanged.