Track the Top 10 URLs by Rolling Average Requests per Second from Apache Logs

Quick Overview

Build a stream processor that parses Apache Common Log Format lines and keeps per-URL request counts over a rolling time window. It tests sliding-window eviction, bounded memory, top 10 ranking with lexicographic tie-breaks, and rounding of average requests per second.

Track the Top 10 URLs by Rolling Average Requests per Second from Apache Logs

Company: Cloudflare

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a system that ingests Apache Common Log Format (CLF) log entries from a stream and maintains a rolling average of the top 10 most frequently requested URLs per second, over a configurable time window. Each log entry is a simplified CLF string in the format `ip - - [timestamp] "METHOD /path HTTP/1.1" status size`, where `timestamp` is a Unix epoch integer. Timestamps in ingested logs are non-decreasing. The system must track per-second request counts for each URL within the rolling window, evict expired seconds automatically, and support querying the top 10 URLs by average requests per second. Ties are broken lexicographically by URL. Memory must not grow without bound, however many log entries are ingested. Implement a class `LogParserRollingAverage(windowSize: int)` with these methods: - `ingest(logLine: str) -> None` ingests one CLF line. It parses the timestamp and URL path, updates the per-second counters, and evicts seconds that have left the rolling window. - `getTopUrls() -> List[str]` returns the top 10 URLs sorted by descending average requests per second over the rolling window, with ties broken lexicographically by URL. - `getAverageRps(url: str) -> float` returns the average requests per second for `url` over the rolling window, rounded to 5 decimal places, or `0.0` if the URL has no requests in the window. - `getWindowSize() -> int` returns the configured window size in seconds. - `getActiveUrlCount() -> int` returns the number of distinct URLs with at least one request in the rolling window. These details fix the expected output: - The URL is the path inside the quoted request. The method, IP, status and size do not affect counting, so `GET /home` and `POST /home` count toward the same URL. - Time comes only from the log lines. With `latest` the largest timestamp ingested so far, the window is the inclusive range `[latest - windowSize + 1, latest]`. Queries never move it. - A URL's average is its request count in the window divided by `windowSize`. The denominator stays `windowSize` even when the stream is younger than the window. - An exact half at the sixth decimal rounds up, so one request in a 64-second window (1 / 64 = 0.015625) gives `0.01563`. - `getTopUrls` returns fewer than 10 URLs when fewer are active, and an empty list when none are. Lexicographic order is plain string comparison, so `"/u10"` sorts before `"/u2"`. Example 1, a 3-second window: ```text monitor = LogParserRollingAverage(3) ingest('10.0.0.1 - - [100] "GET /home HTTP/1.1" 200 512') getAverageRps("/home") -> 0.33333 ingest('10.0.0.2 - - [100] "GET /api/users HTTP/1.1" 200 128') ingest('10.0.0.1 - - [101] "POST /api/users HTTP/1.1" 201 64') ingest('10.0.0.3 - - [102] "GET /home HTTP/1.1" 200 512') ingest('10.0.0.4 - - [102] "GET /about HTTP/1.1" 200 256') getTopUrls() -> ["/api/users", "/home", "/about"] getAverageRps("/home") -> 0.66667 getActiveUrlCount() -> 3 getWindowSize() -> 3 ingest('10.0.0.2 - - [104] "GET /about HTTP/1.1" 200 256') getTopUrls() -> ["/about", "/home"] getAverageRps("/about") -> 0.66667 getAverageRps("/api/users") -> 0.0 getActiveUrlCount() -> 2 ``` After the first line only one second has data, but the average still divides by the window size, and 1 / 3 rounds to 0.33333. At 102 the window is `[100, 102]`. `/api/users` has two requests (the method does not matter) and ties `/home`, so the tie-break puts it first. The line at 104 moves the window to `[102, 104]`, which evicts seconds 100 and 101 and leaves `/about` with 2 requests and `/home` with 1. Example 2, a 1-second window with more than 10 active URLs: ```text monitor = LogParserRollingAverage(1) ingest('10.0.0.9 - - [7] "GET /u1 HTTP/1.1" 200 10') ingest('10.0.0.9 - - [7] "GET /u2 HTTP/1.1" 200 10') ingest('10.0.0.9 - - [7] "GET /u3 HTTP/1.1" 200 10') ingest('10.0.0.9 - - [7] "GET /u4 HTTP/1.1" 200 10') ingest('10.0.0.9 - - [7] "GET /u5 HTTP/1.1" 200 10') ingest('10.0.0.9 - - [7] "GET /u5 HTTP/1.1" 200 10') ingest('10.0.0.9 - - [7] "GET /u6 HTTP/1.1" 200 10') ingest('10.0.0.9 - - [7] "GET /u7 HTTP/1.1" 200 10') ingest('10.0.0.9 - - [7] "GET /u8 HTTP/1.1" 200 10') ingest('10.0.0.9 - - [7] "GET /u9 HTTP/1.1" 200 10') ingest('10.0.0.9 - - [7] "GET /u10 HTTP/1.1" 200 10') ingest('10.0.0.9 - - [7] "GET /u11 HTTP/1.1" 200 10') getTopUrls() -> ["/u5", "/u1", "/u10", "/u11", "/u2", "/u3", "/u4", "/u6", "/u7", "/u8"] getAverageRps("/u5") -> 2.0 getActiveUrlCount() -> 11 ingest('10.0.0.9 - - [8] "GET /u9 HTTP/1.1" 200 10') getTopUrls() -> ["/u9"] getAverageRps("/u5") -> 0.0 getActiveUrlCount() -> 1 ``` `/u5` has two requests and leads. The other ten URLs tie at one request each and sort as strings, so `/u10` and `/u11` come before `/u2`, and `/u9` is the one left out of the top 10. The line at second 8 moves the 1-second window to `[8, 8]`, so everything from second 7 is evicted. ```hint What each second must remember Timestamps never decrease, so the oldest second always leaves the window first. Decide what you need to store about each second so that its requests can be subtracted when it expires. ``` ```hint Where memory can leak List every structure that grows with the number of distinct URLs or seconds, and check that each entry is removed once it falls out of the window. ``` ### Constraints & Assumptions - `1 <= windowSize <= 3600` - Log lines follow the simplified CLF format `ip - - [timestamp] "METHOD /path HTTP/1.1" status size`. - Timestamps in log lines are non-decreasing. - At most `10⁵` log lines will be ingested. - URL paths consist of lowercase letters, digits, slashes, dots, and hyphens. ### Clarifying Questions to Ask - How often is `getTopUrls` called compared with `ingest`? - Roughly how many distinct URLs appear in one second, and in one full window? - What should happen to a line that does not parse: skip it, count it, or raise an error? ### What a Strong Answer Covers - Parsing the timestamp and the path out of each line. - Window state that supports exact eviction, with the window bounds stated precisely. - An argument that memory depends only on the window and the URLs inside it, including removal of URLs whose count drops to zero. - The top 10 query: ranking, the tie-break, fewer than 10 active URLs, and its cost. - The fixed `windowSize` denominator and the rounding to 5 decimal places, including exact halves. ### Follow-up Questions - If `getTopUrls` is called after every `ingest`, how would you avoid sorting all active URLs each time? - How would you handle lines that arrive up to a few seconds out of order? - Several servers each run this ingester. How would you compute the global top 10? - If there are too many distinct URLs to count exactly, what approximate structure would you use, and what error would you accept?

Overview: Build a stream processor that parses Apache Common Log Format lines and keeps per-URL request counts over a rolling time window. It tests sliding-window eviction, bounded memory, top 10 ranking with lexicographic tie-breaks, and rounding of average requests per second.

Cloudflare logo
Cloudflare
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
1
0

Design a system that ingests Apache Common Log Format (CLF) log entries from a stream and maintains a rolling average of the top 10 most frequently requested URLs per second, over a configurable time window.

Each log entry is a simplified CLF string in the format ip - - [timestamp] "METHOD /path HTTP/1.1" status size, where timestamp is a Unix epoch integer. Timestamps in ingested logs are non-decreasing.

The system must track per-second request counts for each URL within the rolling window, evict expired seconds automatically, and support querying the top 10 URLs by average requests per second. Ties are broken lexicographically by URL. Memory must not grow without bound, however many log entries are ingested.

Implement a class LogParserRollingAverage(windowSize: int) with these methods:

  • ingest(logLine: str) -> None ingests one CLF line. It parses the timestamp and URL path, updates the per-second counters, and evicts seconds that have left the rolling window.
  • getTopUrls() -> List[str] returns the top 10 URLs sorted by descending average requests per second over the rolling window, with ties broken lexicographically by URL.
  • getAverageRps(url: str) -> float returns the average requests per second for url over the rolling window, rounded to 5 decimal places, or 0.0 if the URL has no requests in the window.
  • getWindowSize() -> int returns the configured window size in seconds.
  • getActiveUrlCount() -> int returns the number of distinct URLs with at least one request in the rolling window.

These details fix the expected output:

  • The URL is the path inside the quoted request. The method, IP, status and size do not affect counting, so GET /home and POST /home count toward the same URL.
  • Time comes only from the log lines. With latest the largest timestamp ingested so far, the window is the inclusive range [latest - windowSize + 1, latest] . Queries never move it.
  • A URL's average is its request count in the window divided by windowSize . The denominator stays windowSize even when the stream is younger than the window.
  • An exact half at the sixth decimal rounds up, so one request in a 64-second window (1 / 64 = 0.015625) gives 0.01563 .
  • getTopUrls returns fewer than 10 URLs when fewer are active, and an empty list when none are. Lexicographic order is plain string comparison, so "/u10" sorts before "/u2" .

Example 1, a 3-second window:

monitor = LogParserRollingAverage(3)
ingest('10.0.0.1 - - [100] "GET /home HTTP/1.1" 200 512')
getAverageRps("/home")        -> 0.33333
ingest('10.0.0.2 - - [100] "GET /api/users HTTP/1.1" 200 128')
ingest('10.0.0.1 - - [101] "POST /api/users HTTP/1.1" 201 64')
ingest('10.0.0.3 - - [102] "GET /home HTTP/1.1" 200 512')
ingest('10.0.0.4 - - [102] "GET /about HTTP/1.1" 200 256')
getTopUrls()                  -> ["/api/users", "/home", "/about"]
getAverageRps("/home")        -> 0.66667
getActiveUrlCount()           -> 3
getWindowSize()               -> 3
ingest('10.0.0.2 - - [104] "GET /about HTTP/1.1" 200 256')
getTopUrls()                  -> ["/about", "/home"]
getAverageRps("/about")       -> 0.66667
getAverageRps("/api/users")   -> 0.0
getActiveUrlCount()           -> 2

After the first line only one second has data, but the average still divides by the window size, and 1 / 3 rounds to 0.33333. At 102 the window is [100, 102]. /api/users has two requests (the method does not matter) and ties /home, so the tie-break puts it first. The line at 104 moves the window to [102, 104], which evicts seconds 100 and 101 and leaves /about with 2 requests and /home with 1.

Example 2, a 1-second window with more than 10 active URLs:

monitor = LogParserRollingAverage(1)
ingest('10.0.0.9 - - [7] "GET /u1 HTTP/1.1" 200 10')
ingest('10.0.0.9 - - [7] "GET /u2 HTTP/1.1" 200 10')
ingest('10.0.0.9 - - [7] "GET /u3 HTTP/1.1" 200 10')
ingest('10.0.0.9 - - [7] "GET /u4 HTTP/1.1" 200 10')
ingest('10.0.0.9 - - [7] "GET /u5 HTTP/1.1" 200 10')
ingest('10.0.0.9 - - [7] "GET /u5 HTTP/1.1" 200 10')
ingest('10.0.0.9 - - [7] "GET /u6 HTTP/1.1" 200 10')
ingest('10.0.0.9 - - [7] "GET /u7 HTTP/1.1" 200 10')
ingest('10.0.0.9 - - [7] "GET /u8 HTTP/1.1" 200 10')
ingest('10.0.0.9 - - [7] "GET /u9 HTTP/1.1" 200 10')
ingest('10.0.0.9 - - [7] "GET /u10 HTTP/1.1" 200 10')
ingest('10.0.0.9 - - [7] "GET /u11 HTTP/1.1" 200 10')
getTopUrls()                  -> ["/u5", "/u1", "/u10", "/u11", "/u2", "/u3", "/u4", "/u6", "/u7", "/u8"]
getAverageRps("/u5")          -> 2.0
getActiveUrlCount()           -> 11
ingest('10.0.0.9 - - [8] "GET /u9 HTTP/1.1" 200 10')
getTopUrls()                  -> ["/u9"]
getAverageRps("/u5")          -> 0.0
getActiveUrlCount()           -> 1

/u5 has two requests and leads. The other ten URLs tie at one request each and sort as strings, so /u10 and /u11 come before /u2, and /u9 is the one left out of the top 10. The line at second 8 moves the 1-second window to [8, 8], so everything from second 7 is evicted.

Constraints & Assumptions

  • 1 <= windowSize <= 3600
  • Log lines follow the simplified CLF format ip - - [timestamp] "METHOD /path HTTP/1.1" status size .
  • Timestamps in log lines are non-decreasing.
  • At most 10⁵ log lines will be ingested.
  • URL paths consist of lowercase letters, digits, slashes, dots, and hyphens.

Clarifying Questions to Ask Guidance

  • How often is getTopUrls called compared with ingest ?
  • Roughly how many distinct URLs appear in one second, and in one full window?
  • What should happen to a line that does not parse: skip it, count it, or raise an error?

What a Strong Answer Covers Guidance

  • Parsing the timestamp and the path out of each line.
  • Window state that supports exact eviction, with the window bounds stated precisely.
  • An argument that memory depends only on the window and the URLs inside it, including removal of URLs whose count drops to zero.
  • The top 10 query: ranking, the tie-break, fewer than 10 active URLs, and its cost.
  • The fixed windowSize denominator and the rounding to 5 decimal places, including exact halves.

Follow-up Questions Guidance

  • If getTopUrls is called after every ingest , how would you avoid sorting all active URLs each time?
  • How would you handle lines that arrive up to a few seconds out of order?
  • Several servers each run this ingester. How would you compute the global top 10?
  • If there are too many distinct URLs to count exactly, what approximate structure would you use, and what error would you accept?