Katkat

Evaluate IP Access Rules

AmazonSoftware Engineer · Technical Screen

Last updated

This question evaluates knowledge of IPv4 addressing, CIDR subnet matching, and access-control rule resolution, assessing competency in bitwise address reasoning, prefix-length precedence, and rule tie-breaking.

You are given a list of IPv4 access-control rules. Each rule consists of:

  • an action: allow or deny
  • a CIDR block such as 192.168.0.0/16

You are also given a target IPv4 address such as 192.168.1.25.

Determine whether the target IP should be allowed or denied.

Use the following rule semantics so the problem is fully specified:

  1. A rule matches if the target IP belongs to that CIDR block.
  2. If multiple rules match, the rule with the longest prefix length takes precedence.
  3. If multiple matching rules have the same prefix length, the later rule in the input overrides the earlier one.
  4. If no rule matches, return deny.

Implement a function that takes a list of rules and one IPv4 address and returns the final decision.

You may assume all inputs are valid IPv4 addresses and valid CIDR blocks, with prefix lengths from 0 to 32.