Evaluate ACL rules for IP and CIDR
Company: Databricks
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's competency in IP address and CIDR arithmetic, bitwise reasoning, and algorithm design within the Coding & Algorithms category, focusing on IP networking and access-control logic.
Part 1: Determine Whether a Single IPv4 Address Is Allowed by an ACL
Constraints
- 0 <= len(rules) <= 200000
- Each action is either ALLOW or DENY
- Each IP address is a valid IPv4 address
- Each CIDR has a mask length from /0 to /32
- Default behavior is deny if no rule matches
Examples
Input: ([('ALLOW', '10.0.0.0/8'), ('DENY', '10.1.2.0/24')], '10.1.2.3')
Expected Output: True
Explanation: The first rule already matches 10.1.2.3 and allows it. The later DENY rule is ignored.
Input: ([('DENY', '192.168.1.0/24'), ('ALLOW', '192.168.1.42/32')], '192.168.1.42')
Expected Output: False
Explanation: The IP matches the first rule, which is DENY, so the request is denied.
Input: ([('ALLOW', '10.0.0.0/8')], '11.0.0.1')
Expected Output: False
Explanation: No rule matches 11.0.0.1, so the default result is deny.
Input: ([('ALLOW', '0.0.0.0/0')], '255.255.255.255')
Expected Output: True
Explanation: The /0 CIDR contains every IPv4 address.
Input: ([], '1.2.3.4')
Expected Output: False
Explanation: An empty ACL means no rule matches, so the IP is denied.
Hints
- Convert an IPv4 address into a 32-bit integer so containment checks become numeric range checks.
- Because the ACL uses first-match-wins semantics, you should stop scanning as soon as you find the first matching rule.
Part 2: Determine Whether Every IP in a Query CIDR Is Allowed
Constraints
- 0 <= len(rules) <= 2000
- Each action is either ALLOW or DENY
- All IPv4 addresses and CIDR strings are valid
- CIDR mask lengths are from /0 to /32
- Your solution should avoid enumerating every IP in the queried CIDR range
Examples
Input: ([('ALLOW', '10.0.0.0/9'), ('ALLOW', '10.128.0.0/9')], '10.0.0.0/8')
Expected Output: True
Explanation: The two ALLOW rules together cover the entire queried /8, and no earlier DENY blocks any part of it.
Input: ([('DENY', '10.0.0.0/16'), ('ALLOW', '10.0.0.0/8')], '10.0.0.0/8')
Expected Output: False
Explanation: Addresses in 10.0.0.0/16 are denied by the first matching rule, so not every IP in the /8 is allowed.
Input: ([('ALLOW', '10.0.0.0/8'), ('DENY', '10.1.0.0/16')], '10.1.0.0/16')
Expected Output: True
Explanation: Every IP in the query matches the earlier ALLOW rule first, so the later DENY rule never applies.
Input: ([('ALLOW', '192.168.0.0/17')], '192.168.0.0/16')
Expected Output: False
Explanation: Only half of the queried /16 is allowed. The other half matches no rule and is denied by default.
Input: ([('ALLOW', '0.0.0.0/0')], '0.0.0.0/0')
Expected Output: True
Explanation: A single ALLOW /0 rule permits every IPv4 address, so the whole query is allowed.
Hints
- Think about which parts of the query CIDR have not been matched by any earlier rule yet.
- A CIDR block can be represented as one numeric interval [start, end]. Allow rules subtract coverage from the remaining interval set; deny rules fail if they touch any still-unmatched part.