KatInterview

Sequential Allow/Deny Rules Over an IPv4 Block

Greedy & IntervalsAuto-graded · 23 test cases
MediumAuto-gradedGreedy & Intervalsintervalsipv4cidr

Last updated

Track an IPv4 block as disjoint integer intervals while applying ordered allow and deny CIDR rules, deciding whether the block ends up fully covered with no deny hit.

Clearing an IPv4 Block with Ordered Rules

You are given a target IPv4 block written in CIDR notation, plus an ordered list of rules. Each rule is a pair [kind, cidr], where kind is either "allow" or "deny" and cidr is an IPv4 block such as "a.b.c.d/x".

A CIDR block a.b.c.d/x stands for every address that shares its first x bits with a.b.c.d.

Process

Begin with an uncleared region equal to the set of all addresses in the target block. Then handle the rules one at a time, from first to last:

  • allow rule: every address of the uncleared region that also lies in the rule's block is removed from the region. Addresses outside the region are unaffected.
  • deny rule: if the rule's block shares at least one address with the uncleared region as it is at that moment, the process stops immediately and the answer is false.

When every rule has been handled, the answer is true if the uncleared region is empty, and false otherwise.

Task

Implement the function that, given the target block and the rule list, returns true if the target is completely cleared under the process above, and false otherwise.

Notes

  • Rules must be applied strictly in the given order. A deny rule is tested only against what remains uncleared at its turn, not against the original target.
  • Removing part of the region from its middle can leave several separate pieces; these must all be tracked correctly.
  • All strings are valid IPv4 CIDR notation. The address part may have host bits set (for example 10.0.0.7/24); such a block means the same as its network form (10.0.0.0/24).
  • Any rule kind other than "deny" can be treated as "allow".
  • With an empty rule list the target is never cleared, so the result is false.

Constraints

  • The target and every rule block are valid IPv4 CIDR strings with prefix length from 0 to 32.
  • The number of rules is moderate (the intended solution is quadratic in the number of rules at worst).
  • Do not enumerate individual addresses; a block can contain up to 2^32 addresses.
  • Return a boolean.

Examples

Example 1

Input: is_block_cleared('10.0.0.0/30', [['allow', '10.0.0.0/31'], ['allow', '10.0.0.2/31']])

Output: True

Example 2

Input: is_block_cleared('10.0.0.0/30', [['allow', '10.0.0.0/31'], ['deny', '10.0.0.2/31']])

Output: False

Example 3

Input: is_block_cleared('192.168.1.0/24', [['allow', '192.168.0.0/16']])

Output: True

Hints

Hint 1

Every CIDR block is a contiguous range of 32-bit integers, from its network address to its broadcast address. Convert blocks to [start, end] integer pairs.

Hint 2

Store the uncleared region as a list of disjoint closed intervals. A deny rule fires if its interval overlaps any stored interval, which is when left <= rule_end and rule_start <= right.

Hint 3

Subtracting [rule_start, rule_end] from [left, right] leaves at most two pieces: [left, rule_start - 1] if left < rule_start, and [rule_end + 1, right] if rule_end < right. Once the list is empty, the answer is true.