Problem · Array

Ordered IP Firewall

Learn this problem
MediumDatabricks logoDatabricksFULLTIMEPHONE SCREEN

Problem statement

You are given an ordered IPv4 firewall. Rule i has action actions[i], which is either "ALLOW" or "DENY", and target cidrs[i], which is either one IPv4 address or an IPv4 CIDR network.

Evaluate the query address against the rules in order. A plain address matches only itself. A CIDR target a.b.c.d/p matches exactly the addresses whose first p bits equal the target network's first p bits. Return true for the first matching ALLOW rule and false for the first matching DENY rule.

Function

allowAccess(actions: String[], cidrs: String[], ip: String) → boolean

Examples

Example 1

actions = ["DENY","ALLOW"]cidrs = ["10.0.0.0/8","0.0.0.0/0"]ip = "10.4.5.6"return = false

The address is inside 10.0.0.0/8, so the first rule matches and denies access.

Example 2

actions = ["ALLOW","DENY","ALLOW"]cidrs = ["192.168.1.9","192.168.1.0/24","0.0.0.0/0"]ip = "192.168.1.9"return = true

The exact-address rule appears before the broader deny rule, so its allow decision wins.

Constraints

  • 1 <= actions.length = cidrs.length <= 200000.
  • Every action is "ALLOW" or "DENY".
  • Every target and query is a valid dotted-decimal IPv4 address; a CIDR prefix is between 0 and 32.
  • At least one rule matches the query.

More Databricks problems

drafts saved locally
public boolean allowAccess(String[] actions, String[] cidrs, String ip) {
    // Write your code here.
}
actions["DENY","ALLOW"]
cidrs["10.0.0.0/8","0.0.0.0/0"]
ip"10.4.5.6"
expectedfalse
checking account