Top N clients in an access log
Count requests per address and rank them
Coding, Easy
Small real code that works when called late, twice, or with bad input.
Write topIps(lines, n) in JavaScript (or the same idea in another language, or a shell pipeline). lines is a list of access-log lines. Each line starts with the client address, then a space, then the rest of the line (for example 10.0.0.5 GET /home 200). Return the n addresses that appear most often, as a list of objects { ip, count }, with the most frequent first. If two addresses have the same count, the one that is smaller as text comes first. If there are fewer than n addresses, return all of them. Skip lines that are empty or contain only spaces.
Examples
Constraints
- 0 <= number of lines <= 1,000,000. Each line is at most 1,000 characters.
- 0 <= n. If n is 0, return an empty list.
- Compare addresses as plain text. Do not parse them as numbers.
- Do not read a line twice, and do not keep the lines in a second copy.
Your answer must contain
- The code (or the shell pipeline)
- How you get the address from a line and what you do with a blank line
- The order rule and how you apply it, including ties
- Time and memory cost, in one or two sentences
- A short trace of one example
Target A strong answer covers one counting pass, the tie rule by text, blank lines, n larger than the count, and the cost of the sort.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
What do you need to know about each address? Think about the one number that lets you rank them.
Hint 2 the approach
Read the lines once. Take the first word of each non-blank line as the address and keep a count for it in a table.
Hint 3 the key step
Sort the table by count, highest first, and by address as text when counts tie. Then cut the list to n. Do not compare addresses as numbers.
Walkthrough
Full walkthrough brute force to best
Naive version
For each address, scan all the lines again and count its lines. That reads the log once per different address, so it costs O(L x D) for L lines and D different addresses. With a million lines this is far too slow.
Best method
Count in one pass with a map from address to number. Take the first word of each trimmed line as the address, and skip lines with no word. Then sort the map entries by count, highest first, and by address as text when counts tie, cut the list to n, and return objects. The tie rule makes the result the same on every run. When n is small and there are many different addresses, a heap of size n gives O(L + D log n), but the plain sort is accepted.
Worked example
Run the second example. Counting gives b 2, a 2 and c 1. Sorting by count, highest first, puts a and b before c. The addresses a and b tie at 2, so the smaller text, a, comes first. The result is a with 2, b with 2, then c with 1.
Reference code
// Each line starts with the client address, then a space, then the rest. Blank lines are skipped.
function topIps(lines, n) {
const counts = new Map();
for (const line of lines) {
const ip = line.trim().split(/\s+/, 1)[0];
if (!ip) continue;
counts.set(ip, (counts.get(ip) ?? 0) + 1);
}
return [...counts.entries()]
.sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0)) // most requests first; ties by address
.slice(0, Math.max(0, n))
.map(([ip, count]) => ({ ip, count }));
}Time and memory
O(L) time to count L lines, plus O(D log D) to sort D different addresses. O(D) memory for the table.
Common mistakes
- Sorting by count only. Ties then come out in any order, and results change between runs.
- Counting blank lines as an address with an empty name.
- Comparing 10.0.0.10 and 10.0.0.1 as numbers. The task says to compare them as text.
- Cutting the list to n before sorting. You then rank the wrong addresses.