KatInterview

Aggregate HTTP Logs to Identify Top Client Addresses

StringsModel answer
MediumModel answerStringsparsingregexaggregation

Last updated

Parse multi-file request logs to extract and rank the five IPv4 addresses that generated the most completed HTTP requests.

You're investigating an API traffic spike and need to identify which client addresses generated the most load. You have access to log data split across multiple files, each containing a mix of HTTP request records and operational messages.

Write a function top_request_ips(files: string[][]) -> string[][] that processes these logs and returns the top five client IPv4 addresses by request volume.

Input Format

  • files is an array of file contents; each file is represented as an array of log lines (strings)
  • Maximum 20 files, 200,000 total lines, 20,000,000 total characters

What Counts as a Request

A line represents a completed HTTP request if it contains all of these elements in order:

  1. An HTTP method: GET, POST, PUT, PATCH, DELETE, HEAD, or OPTIONS (as a complete word)
  2. A URL (non-whitespace characters)
  3. An HTTP version: HTTP/1.0, HTTP/1.1, or HTTP/2.0
  4. The literal text from
  5. An IPv4 address in dotted-decimal notation (0-255 per octet, no leading zeros except 0 itself)
  6. A colon and port number (1-65535)
  7. A space, hyphen, space, then exactly three digits (the HTTP status code)

Example valid request line:

time="t" msg="GET https://api.example/users HTTP/1.1 from 192.168.1.5:443 - 200 1.2KB"

Lines that mention an IP address but lack the full request structure should be ignored. For instance, Client IP="10.0.0.5:80" redirect diagnostics or background messages like "scheduler from 10.0.0.1" do not count.

Counting Rules

  • Count each matching line exactly once
  • Group by IP address only; ignore the port number
  • Count all status codes (2xx, 4xx, 5xx, etc.)
  • The same IP appearing in different files or with different ports contributes to a single total

Output Format

Return an array of [ip, count] pairs, where:

  • count is a decimal string
  • Results are ordered by count (descending), then by numeric IPv4 value (ascending) for ties
  • Numeric IPv4 comparison treats each octet as an integer: 10.0.0.9 comes before 10.0.0.10
  • Return fewer than five rows if fewer than five distinct IPs are found

Example

files = [[
  'time="t" msg="GET https://api.example/a HTTP/2.0 from 10.0.0.2:12 - 200 4B"',
  'time="t" msg="redirecting" Client IP="10.0.0.9:15"',
  'time="t" msg="POST https://api.example/b HTTP/1.1 from 10.0.0.2:13 - 500 0B"'
], [
  'time="t" msg="GET https://api.example/c HTTP/1.1 from 10.0.0.10:14 - 200 4B"',
  'time="t" msg="no pending executions"'
]]

// Result: 10.0.0.2 appears twice (200 and 500), 10.0.0.10 once
// The "Client IP=" line is ignored
result = [["10.0.0.2","2"], ["10.0.0.10","1"]]

Discussion Questions

  1. How would you process logs stored as compressed archive files without loading everything into memory?
  2. What additional signals would you examine before concluding that a high-volume IP address is malicious?

Hints

Hint 1

Use regex to match the full request pattern, not just the presence of an IP address

Hint 2

A cheap substring precheck like `' from ' in line` can skip most non-request lines before running the full regex

Hint 3

For numeric IPv4 ordering, split each address into four integers and compare those tuples