Practice problem · Databricks · Medium

CIDR Firewall

A firewall decides whether traffic from an IPv4 address gets through. 3 stages in Python with an AI assistant. Stage 1 free.

Stage 1 of 3: First matching rule

Background. A firewall decides whether traffic from an IPv4 address gets through. Its rules form an ordered list; each pairs an action, "allow" or "deny", with a block of addresses.

Implement Firewall(rules) and Firewall.check(address) in firewall.py.

  • rules is a list of (action, pattern) pairs, possibly empty. A pattern is a block n/p, such as "198.51.100.0/24", or a bare address such as "198.51.100.7", which stands for the block of that one address, the same as /32.
  • check(address) takes an address such as "198.51.100.23" and returns the action of the first rule in the list whose block holds it, or "deny" when no rule holds it.

An address is read as one 32-bit number whose most significant byte is its first number. The block n/p holds every address whose top p bits are the same as the top p bits of n. The low 32 - p bits of n play no part: "198.51.100.77/24" is the same block as "198.51.100.0/24". So /0 holds every address and /32 exactly one.

Example

firewall = Firewall([
    ("deny", "203.0.113.128/25"),
    ("allow", "203.0.113.0/24"),
    ("allow", "198.51.100.7"),
])
firewall.check("203.0.113.200")  # "deny": the first rule holds it
firewall.check("203.0.113.5")    # "allow": the second rule is the first to hold it
firewall.check("198.51.100.7")   # "allow"
firewall.check("198.51.100.8")   # "deny": no rule holds it

Notes

  • Inputs are always valid: an address is four numbers from 0 to 255 with no leading zeros or spaces, a length is 0 to 32, and an action is exactly "allow" or "deny".
  • On the starter, all 9 tests in tests/test_part1.py fail.

Done when Submit goes green.

Constraints

  • Python 3.12 standard library only
  • Editable: firewall.py
  • Read-only: all tests/test_*.py files
  • Inputs are always valid: addresses are four numbers 0 to 255, lengths 0 to 32, actions "allow" or "deny"

What comes after stage 1

The next stages open once the tests for the one before them pass. Each raises the bar, the way an interviewer does in the room.

  1. Stage 2: Many rules, many checks. Tens of thousands of rules and as many checks, and every check still has to be quick.
  2. Stage 3: Whole blocks. The thing to check becomes a whole block of addresses, up to the entire address space.

The files you get

  • Yours to edit: firewall.py.
  • Tests you can read and run: tests/test_part1.py. Hidden tests run on Submit.

The round: an agent in the editor

The assistant here works like the one in the interview IDE. It reads your files, runs code and proposes changes, and you accept or reject each one. It doesn't volunteer ideas, so what gets built is what you asked for. Along the way the interviewer asks about your decisions, and at the end the report says what you checked and what you accepted unread.

Frequently asked, briefly answered

Is CIDR Firewall free?

Stage 1 is free with an account. The other 2 stages need Nyrion Pro.

Which company asks something like this?

Databricks. Based on candidates' reports of Databricks interviews. Nyrion wrote this version, so the question you get may differ.

Do I bring my own AI tool?

No. The assistant is in the editor, same as in the interview IDEs at Meta, Stripe and Amazon. What you choose at Begin is the format: an agent that proposes changes you accept or reject, or an answer-only assistant that can't write code.

What does the interviewer ask?

6 questions along the way about your own code, and one at the end about the whole session. A weak answer gets one follow up. The answers go into the report, which ends with a hire or no hire verdict.

Start the problem

Sign in and the problem opens where you left it. New here? An account takes a minute to create, and the first stage of this problem is free.

Start this problem → Sign in

More like it: every practice problem, how Databricks runs its AI round, the AI coding interview guide.