Evaluate IP Access Rules
AmazonSoftware Engineer · Technical ScreenLast 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:
allowordeny - 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:
- A rule matches if the target IP belongs to that CIDR block.
- If multiple rules match, the rule with the longest prefix length takes precedence.
- If multiple matching rules have the same prefix length, the later rule in the input overrides the earlier one.
- 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.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.