KatInterview

HTTP Field Extraction with IP Address Frequency Analysis

StringsModel answer
EasyModel answerStringsstring-parsinghttpvalidation

Last updated

Retrieve text fields from multiple endpoints, parse and validate IPv4 addresses, then rank them by occurrence frequency.

You are building a monitoring tool that analyzes log data distributed across multiple services. Your task is to write a script that collects data from HTTP endpoints, extracts IP addresses from the responses, and produces a frequency report.

Your script must:

  1. Fetch data from a given endpoint: Make an HTTP GET request to a provided URL, including a specified header (name and value will be given), and extract the value of a designated field from the JSON response.

  2. Parse and validate IP addresses: From the extracted text, identify all valid IPv4 addresses. An address is valid if it consists of exactly four decimal integers separated by dots, where each integer is between 0 and 255 inclusive, with no leading zeros (except for the standalone digit 0).

  3. Process a second endpoint: Repeat the extraction for a second URL (using the same header and field name).

  4. Generate a frequency report: Combine results from both sources, count how many times each distinct IP address appears, and output the addresses sorted by count in descending order. For addresses with the same count, sort them lexicographically.

Input Specification

You will be given:

  • Two URLs (strings)
  • A header name and value (strings)
  • A field name to extract from the JSON response (string)

Output Specification

Print each distinct IP address followed by its total count, sorted by count (descending), then by address (ascending) as a tiebreaker.

Additional Requirements

  • Handle HTTP errors gracefully (timeouts, non-200 status codes, malformed JSON)
  • Use word boundaries when extracting IPs—don't match partial segments from longer numeric sequences
  • The extracted field contains free-form text; IP addresses may appear mixed with other content
  • Return addresses in the order they appear for intermediate steps (before final counting)

Example

If the first endpoint returns {"logs": "Connection from 192.168.1.1 failed, retry from 10.0.0.5"} and the second returns {"logs": "Request from 192.168.1.1 succeeded"}, your output should be:

192.168.1.1: 2
10.0.0.5: 1

Clarification Points to Confirm

  • Can the specified field be missing or null?
  • Should both endpoints use the same header?
  • What should happen if one endpoint fails—return partial results or fail entirely?
  • Are IPv6 addresses in scope, or only IPv4?
  • How should sequences like 1.2.3.4.5 be handled?

Hints

Hint 1

Use regular expressions with word boundaries to avoid matching IP-like substrings within longer numeric sequences

Hint 2

A dictionary or hash map is perfect for counting occurrences across multiple sources

Hint 3

Consider extracting the HTTP fetch and field extraction logic into reusable functions to avoid duplication