AO
Back

IP CIDR Firewall Rules Matching

CodingPhone, OnsiteLast reported August 2026High Frequency

Problem Overview

Databricks' CIDR firewall question, reported as a phone screen and as an onsite algorithm round. A firewall is an ordered list of rules. Each rule is an action, "ALLOW" or "DENY", and a CIDR block. A block a.b.c.d/N covers every IPv4 address whose first N bits (of 32) equal the first N bits of a.b.c.d; a rule can also be a plain IP such as "8.8.8.8", which covers that one address. One candidate quoted a firewall of DENY 255.0.0.8/29 and ALLOW 117.145.102.64/30; another gave the rules ("192.168.1.0/24" : "ALLOW"), ("8.8.8.8" : "DENY"), under which 192.168.1.5 is allowed.

Our setup — ask your interviewer. Reports name the API differently: a prep-site statement that one candidate said matched their question has a class whose allowAccess method takes the IP and returns a boolean, and another report has a function get_status that takes the rules and the IP and returns the status. The tests here call:

class IpFirewall:
    def __init__(self, rules: list): ...        # rules = [[action, cidr], ...] in priority order
    def allow_access(self, ip: str): ...         # Part 1
    def allow_access_cidr(self, cidr: str): ...  # Part 2 (or let allow_access take a CIDR)

Each answer may be True/False or 'ALLOW'/'DENY'; the tests accept both.

Part 1 — Single IP, first match wins

allow_access(ip) returns the action of the first rule, in list order, whose block contains ip. A later rule that overlaps an earlier one never overrides it. The prep-site statement guarantees that every queried IP matches some rule, and the Part 1 tests only query addresses that do. The tests build firewalls with IpFirewall(rules) and call allow_access(ip) with a dotted IPv4 string.

Part 2 — CIDR query, whole block must be allowed

Reported in about 13 threads; sometimes discussed rather than coded. The query is now a CIDR block, and it is allowed only if every address in it is allowed. The rules are still taken in order: an address that no rule matches is not allowed, and a DENY rule only matters for the addresses that no earlier rule has already decided. So a block that the ALLOW rules cover between them is still denied when a DENY ahead of one of those ALLOW rules hits part of it. One report words the follow-up differently (the block must satisfy every rule); this page follows the in-order reading another report spells out, so ask your interviewer. Write allow_access_cidr(cidr), or make allow_access accept a CIDR as one report describes: the tests call allow_access_cidr if your class defines it and allow_access otherwise.

Checking every address one by one is the brute force: a block /N holds 2^(32−N) addresses. In one report the interviewer asked for a data structure that makes range checks easy; the two candidates who report proposing the brute force both still passed. The tests use small blocks and do not grade speed.

Not graded

  • Variant: one report frames the screen as a function, get_status, taking the rules as (cidr, status) pairs and an IP, returning the status string of the first rule that matches.
  • Follow-ups reported without an answer: what other test cases you would write, and how you would scale it.

Follow-up Arc

Interviewers escalate through these phases. The order varies, but most candidates see at least one from each bucket.
Concurrency · 1Trade-off discussion · 3
Concurrency

What if the query is a CIDR block instead of a single IP? When should it return ALLOW?

Probes for: Candidate completes base IP-vs-CIDR matching
Trade-off discussion

What test cases would you write for this?

Probes for: After base solution is written

Can you write pseudocode for the CIDR-vs-CIDR matching condition and the overlap check?

Probes for: Candidate has given CIDR follow-up approach

How would you scale this?

Probes for: System is working for small inputs

Approach Trade-offs

Approaches actually attempted in reports — including ones that lost candidates time. Pick deliberately.
ApproachNotes
Binary trie over rule prefixesForum commenters suggested a trie for O(32) lookups instead of an O(R) scan, and another asked how a trie keeps the rule order; nobody answered in the thread. Our analysis: store at each node the smallest index of a rule ending there and take the smallest index seen along the query's 32-bit path, which keeps first-match order. One commenter who tried a trie for the follow-up could not finish it.
Integer ranges with interval subtractionEach CIDR is a contiguous integer range, so the follow-up becomes an interval problem. One candidate compared it to Range Module; a commenter calls it an ordinary interval problem with no segment tree needed. Worst case O(R^2) for R rules.
Brute force enumeration (follow-up only)Apply the single-IP check to every address in the query block. It gives the same answers but costs 2^(32-N) lookups for a /N block, which is large for short prefixes (a /8 is about 16.7 million addresses). In one report the interviewer then asked for a data structure for range checks; the two candidates who report proposing it both passed.

What Reports Emphasize

Common mistakes: Bit-operator precedence bugs, such as a missing pair of parentheses in (1 << N) - 1: one candidate wrote the solution quickly, then debugged for a long time over operator precedence.; Not finishing the base problem fast enough: one candidate whose tests passed had no time left for the follow-up and was rejected.; Proposing only brute-force enumeration for the follow-up: in one report the interviewer then asked for a data structure for range checks (that candidate still passed).; Code that passes but is not explained: one candidate got feedback that their explanation was not clear enough, was given an extra round, and passed it.; Checking only whether the ALLOW rules together cover the block: one report says the rules must be processed in order, because a DENY earlier in the list can make the answer false.; Signed comparisons (a reported implementation issue, not an edge case the interviewer probed): one report that probably describes this round blames 'the 2's complement thing' for comparisons that went wrong.

Interviewer hints: In four reports, the interviewer explained CIDR and checked the candidate's understanding with examples before coding.; In one report, after the candidate described brute-force enumeration for the follow-up, the interviewer asked whether some data structure makes range checks easy.; In one report, with time running short, the interviewer asked for pseudocode for the follow-up.; One interviewer asked the candidate to explain several parts of the code while they wrote it.

What passers do: Doing the base problem fast enough to reach the follow-up: one poster says the follow-up had to be implemented and to do the first part quickly.; Treating each CIDR as an integer range and the follow-up as an interval problem (one candidate compared it to Range Module).; Explaining the bit manipulation while writing it: one candidate whose explanation was called unclear passed an extra round after explaining carefully.; Making sure you understand CIDR before coding: in four reports the interviewer explained it and checked with examples first.

Why people fail: Writing slowly or debugging for long and not reaching the follow-up (two rejected reports); Going over time on the base problem even with passing tests: one algorithm round was failed and redone; Passing every test with one initialization line wrong: one candidate was rejected; Rusty bit masking: one candidate who failed the onsite says they did not do the CIDR question well

Edge cases probed: Overlapping rules: a later entry never overrides an earlier one (stated in one report's prompt); A plain IP as a rule, covering a single address (in two reports' examples); A query block that is partly allowed and partly denied (follow-up, one report); One interviewer kept asking about corner cases; the report does not list them

Practice

Write your own against 10 test cases, or read the worked solution — approach, complexity, and code that runs.

Prepping for Databricks with friends?

Send them this page. It is free to read, no account needed.

Free preview

Every question in the Databricks catalog gets this depth

What you just read — canonical solution, follow-up arc, what passing candidates actually did — exists for all 34 Databricks questions, refreshed monthly from new candidate reports.

$59/mo — or $50/mo with the 3-month pass · cancel anytime
Databricks · Coding · Reported 34× across candidate reports
Is this helpful?