FastPrepAggregate Matching IPv4 Addresses Across Responses

Aggregate Matching IPv4 Addresses Across Responses

Pinterest logoPinterest● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Two HTTP response bodies have already been split into text lines. Extract whitespace-delimited canonical IPv4 tokens from firstResponseLines and use their distinct values as the target set. Then count every occurrence of a target IPv4 token in secondResponseLines.

A canonical IPv4 token has exactly four decimal octets from 0 through 255. An octet is 0 or has no leading zero. Ignore every other token.

Return one string "ip count" for each target that occurs in the second response, ordered by decreasing count and then by numeric IPv4 order.

Function

aggregateIpCounts(firstResponseLines: String[], secondResponseLines: String[]) → String[]

Examples

Example 1

firstResponseLines = ["allow 10.0.0.1 192.168.1.2","duplicate 10.0.0.1"]secondResponseLines = ["10.0.0.1 8.8.8.8 10.0.0.1","192.168.1.2"]return = ["10.0.0.1 2","192.168.1.2 1"]

Only targets from the first response are counted.

Example 2

firstResponseLines = ["1.2.3.4 1.2.3.5"]secondResponseLines = ["1.2.3.5 1.2.3.4"]return = ["1.2.3.4 1","1.2.3.5 1"]

Equal counts use numeric address order.

Constraints

  • Each input contains at most 20000 lines and 200000 total tokens.
  • Each token contains at most 100 characters.
  • Counts fit in a signed 32-bit integer.

More Pinterest problems

See Pinterest hiring insights
public String[] aggregateIpCounts(String[] firstResponseLines, String[] secondResponseLines) {
    // Write your code here.
}
firstResponseLines["allow 10.0.0.1 192.168.1.2","duplicate 10.0.0.1"]
secondResponseLines["10.0.0.1 8.8.8.8 10.0.0.1","192.168.1.2"]
expected["10.0.0.1 2", "192.168.1.2 1"]
Checking account…