KatInterview

Busiest Network Addresses With Tie-Inclusive Top Ten Cutoff

Arrays & HashingAuto-graded · 21 test cases
MediumAuto-gradedArrays & Hashinghash-mapcountingsorting

Last updated

Normalize IPv4/IPv6 strings, skip invalid ones, count occurrences, and return the top ten addresses while keeping every address tied at the tenth-place frequency.

Busiest Addresses

You are given a list of strings, each meant to be the textual form of an IP address (IPv4 or IPv6). Report the most frequently seen addresses, keeping everyone tied at the cutoff.

Task

Implement a function that takes the list entries and returns a list of normalized address strings.

  1. Normalize each entry: strip leading and trailing whitespace, parse it as an IPv4 or IPv6 address, and convert it to its canonical compressed text form. For example, 2001:0db8::1 and 2001:db8:0:0:0:0:0:1 are the same address and both become 2001:db8::1.
  2. Skip invalid entries. An entry that cannot be parsed as an IP address is ignored and counts toward nothing.
  3. Count how many entries map to each normalized address.
  4. Order the distinct addresses by descending count. Addresses with equal counts are ordered by their normalized string in ascending lexicographic (plain string comparison) order.
  5. Select:
    • If there are at most ten distinct addresses, return all of them in that order.
    • Otherwise, let cutoff be the count of the address in the tenth position of that order. Return every address whose count is at least cutoff, in that same order. The result may therefore contain more than ten addresses.

If no entry is valid, return an empty list.

Follow-up to think about (not tested)

How would you solve this if the distinct-address map did not fit in memory? Consider hash-partitioning the stream to disk so equal addresses land in the same partition, counting each partition exactly, then applying a global threshold, and what to do about a skewed partition.

Constraints

  • 0 <= len(entries) <= 10^5 for the tested inputs.
  • Entries may be IPv4, IPv6 in any valid textual form, or arbitrary invalid text.
  • Counts can be large, so do not assume a fixed small integer width in a general solution.
  • Output uses the canonical compressed form of each address.

Examples

Example 1

Input: busiest_addresses(['192.168.1.1', '10.0.0.1', '192.168.1.1', '8.8.8.8', '10.0.0.1', '192.168.1.1'])

Output: ['192.168.1.1', '10.0.0.1', '8.8.8.8']

Example 2

Input: busiest_addresses(['2001:0db8::1', '2001:db8:0:0:0:0:0:1', 'bad-ip', '127.0.0.1', '127.0.0.1', '::1'])

Output: ['127.0.0.1', '2001:db8::1', '::1']

Example 3

Input: busiest_addresses(['1.1.1.1', '2.2.2.2', '3.3.3.3', '4.4.4.4', '5.5.5.5', '6.6.6.6', '7.7.7.7', '8.8.8.8', '9.9.9.9', '10.10.10.10', '11.11.11.11', '12.12.12.12'])

Output: ['1.1.1.1', '10.10.10.10', '11.11.11.11', '12.12.12.12', '2.2.2.2', '3.3.3.3', '4.4.4.4', '5.5.5.5', '6.6.6.6', '7.7.7.7', '8.8.8.8', '9.9.9.9']

Hints

Hint 1

Parsing and normalizing is the key to correct grouping: two different strings can denote the same address. Python's standard library has a module that parses and prints addresses canonically.

Hint 2

Count normalized strings in a dictionary, then sort the (address, count) pairs by the key (-count, address) so ties are resolved deterministically.

Hint 3

Once sorted, look at the tenth item's count and keep everything whose count is at least that value. If there are ten or fewer distinct addresses, just return them all.