PIXELBANKv9.1.0
Menu

Problem Statement

Before peering VPCs, check whether any two CIDR blocks overlap (which would break routing). Return whether the given set of blocks is conflict-free.

Background

Each CIDR maps to an integer address range [network_base, network_base + 2(32-prefix) - 1]** where network_base is the network address with host bits zeroed. Two ranges overlap if start1 <= end2 and start2 <= end1. The set is valid if no pair overlaps.

Your Task

def has_overlap(cidrs):
  • cidrs: list of "network/prefix" strings.
  • Return True if any two blocks overlap, else False.

Input Format

  • cidrs (list of str).

Output Format

  • A boolean.

Sample

print(has_overlap(["10.0.0.0/16", "10.0.1.0/24"]))

Output:

True

Example:

Input:
print(has_overlap(["10.0.0.0/16", "10.0.1.0/24"]))
Output:
True
Reasoning:
  • Convert the first CIDR 10.0.0.0/16 to an integer range. The IP 10.0.0.0 converts to the integer 167772160167772160. With a prefix of 16, the block size is 232βˆ’16=655362^{32-16} = 65536. The network base is 167772160167772160 (host bits are already zero), so the range is [167772160,167837695][167772160, 167837695].
  • Convert the second CIDR 10.0.1.0/24 to an integer range. The IP 10.0.1.0 converts to the integer 167772416167772416. With a prefix of 24, the block size is 232βˆ’24=2562^{32-24} = 256. The network base is 167772416167772416, so the range is [167772416,167772671][167772416, 167772671].
  • Sort the ranges by their starting addresses to facilitate efficient comparison. The ranges are already in order: [167772160,167837695][167772160, 167837695] followed by [167772416,167772671][167772416, 167772671].
  • Check for overlap between adjacent ranges. Compare the start of the second range (167772416167772416) with the end of the first range (167837695167837695). Since 167772416≀167837695167772416 \le 167837695, the condition for overlap is met.
  • The final output is True

Constraints:

  • Zero host bits to get the true network base.
  • Ranges overlap iff start1 <= end2 and start2 <= end1.
  • Return True if any pair overlaps.
πŸ”’

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.