KatInterview

Ordered IPv4 Access List: Verdict for an Address or Entire Subnet

Math & Bit ManipulationAuto-graded · 30 test cases
HardAuto-gradedMath & Bit Manipulationcidrbit-manipulationstack

Last updated

Evaluate an ordered first-match IPv4 access list for a single address or a whole CIDR block, which needs prefix arithmetic and interval reasoning rather than enumerating addresses.

You are given an access list: an ordered sequence of entries, each pairing an IPv4 pattern with a verdict ("ALLOW" or "DENY"). For example:

acl = [("192.168.1.0/24", "ALLOW"), ("8.8.8.8", "DENY")]

Your task is to decide whether a given target is allowed.

Patterns

  • A pattern of the form a.b.c.d/p is a CIDR block. It covers every address whose leading p bits equal the leading p bits of a.b.c.d.
  • A pattern with no / is a single address and behaves exactly like /32.
  • Treat an address as a 32-bit unsigned integer, with the first octet as the most significant byte.
  • /0 covers every address.
  • Bits of the written address beyond the first p are ignored. For example, 10.0.0.5/24 behaves exactly like 10.0.0.0/24.

Verdict of one address

The verdict of an address is the verdict of the earliest entry in the list (lowest index) whose pattern covers it. If no entry covers the address, its verdict is "DENY".

The target

The target is either a bare address (a block holding exactly one address) or a CIDR block a.b.c.d/q, which stands for every address whose first q bits match those of a.b.c.d.

Return "ALLOW" if every address in the target block has verdict "ALLOW". Otherwise return "DENY". For a bare address, this is simply that address's verdict.

Note that a block can fail either because some address in it hits a DENY entry first, or because some address in it is not covered by any entry at all.

Constraints

  • 1 <= len(acl) <= 10^5
  • Every prefix length, in an entry or in the target, is an integer from 0 to 32. A pattern with no / means /32.
  • Every address is four decimal octets from 0 to 255 separated by dots, with no leading zeros or whitespace.
  • Every verdict is exactly "ALLOW" or "DENY".
  • A target block may contain up to 2^32 addresses, so checking addresses one by one is not feasible.
  • The return value is exactly "ALLOW" or "DENY".

Examples

Example 1

Input: evaluate_access([['192.168.1.0/24', 'ALLOW'], ['8.8.8.8', 'DENY']], '192.168.1.5')

Output: 'ALLOW'

Example 2

Input: evaluate_access([['10.0.0.7', 'DENY'], ['10.0.0.0/24', 'ALLOW']], '10.0.0.0/28')

Output: 'DENY'

Example 3

Input: evaluate_access([['10.0.0.0/25', 'ALLOW'], ['10.0.0.128/25', 'ALLOW']], '10.0.0.0/24')

Output: 'ALLOW'

Hints

Hint 1

Any two CIDR blocks are either nested (one contains the other) or completely disjoint. They never partially overlap.

Hint 2

Split the entries by size relative to the target. Entries at least as large as the target either contain all of it or none of it, and only the earliest containing one matters. Entries strictly smaller are either fully inside the target or irrelevant.

Hint 3

Sort the relevant smaller blocks by (start, prefix length) and walk them with a stack that holds the current chain of nested blocks. For each block, compare the total size of its direct children against its own size to detect addresses not covered by any child, and carry the minimum entry index along the chain.