Rolling Up Page Views Through Hostname Suffixes
StringsAuto-graded · 22 test casesLast updated
Aggregate per-host view counts so every host and each of its parent suffixes gets the total of itself and all hosts beneath it, using suffix enumeration and a hash map.
Rolling Up Page Views Through Hostname Suffixes
A web analytics service records traffic as lines of text. Each line has the form "<views> <hostname>", for example "900 google.com". A hostname is a sequence of labels separated by dots. Each hostname sits beneath every suffix made of its trailing labels: mail.google.com is beneath google.com, which is beneath com.
Write a function that computes, for every hostname that appears in the hierarchy, the total number of views for that hostname plus all hostnames beneath it.
Task
Given a list of record strings, return a dictionary that maps each hostname to its rolled-up view total.
- The result must contain every hostname that appears in an input record, and also every dot-separated suffix of each such hostname (made of whole trailing labels).
- The total for a key is the sum of the view counts of all input records whose hostname is equal to that key or ends with
.followed by that key. - If the same hostname appears in several records, their counts are added together.
- Suffixes are formed on label boundaries only. For instance,
oogle.comis not a suffix ofgoogle.com. - A hostname with a single label (such as
com) has only itself as a suffix. - The order of keys in the returned dictionary does not matter.
Input
records: a list of strings, each in the format"<views> <hostname>", with the count and hostname separated by whitespace.
Output
- A dictionary mapping each hostname (string) to its total view count (integer).
Constraints
- 1 <= number of records <= 10,000
- Each hostname has length between 1 and 100 characters
- 1 <= views per record <= 1,000,000
- Hostnames may repeat across records, and different hostnames may share suffixes
Examples
Example 1
Input: roll_up_views(['900 google.com', '50 mail.google.com'])
Output: {'google.com': 950, 'com': 950, 'mail.google.com': 50}
Example 2
Input: roll_up_views(['10 apple.com', '20 banana.com'])
Output: {'apple.com': 10, 'com': 30, 'banana.com': 20}
Hints
Hint 1
You do not need to build an explicit tree. Think about which keys a single record contributes to.
Hint 2
A record for a.b.c contributes its count to a.b.c, b.c and c. Split the hostname on dots and generate each trailing slice of the label list.
Hint 3
Use a dictionary and add to the existing value (rather than overwriting) so repeated hostnames and shared suffixes accumulate correctly.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
A visit to a hostname counts for that hostname and for every parent suffix above it. So a record for mail.google.com contributes its views to mail.google.com, google.com and com. That means we can skip building a tree and simply enumerate suffixes.
For each record:
- Split on whitespace to get the view count and the hostname, and convert the count to an integer.
- Split the hostname on
.into labels. - For every starting index
i, joinlabels[i:]with dots to form a suffix. - Add the count to that suffix's running total in a dictionary.
After processing all records, the dictionary holds the rolled-up total for every key.
Why it is correct
Take any key K in the output. Its correct total is the sum of counts from every record whose hostname equals K or is beneath it. A hostname X is beneath K (or equal) exactly when K is a label-wise suffix of X. The algorithm adds X's count to each label-wise suffix of X exactly once and to nothing else, so K receives exactly the right contributions. Splitting on dots guarantees we never create partial-label suffixes like oogle.com. Adding to the existing value (instead of assigning) makes repeated hostnames accumulate.
Complexity
Let N be the number of records, L the maximum number of labels in a hostname, and S the maximum hostname length.
- Time: O(N * L * S), since each record yields up to
Lsuffixes and each is built and hashed in O(S). - Space: O(D * S), where
D <= N * Lis the number of distinct suffix keys.
Reference solution
from typing import Dict, List
def roll_up_views(records: List[str]) -> Dict[str, int]:
totals = {}
for entry in records:
count_text, host = entry.split()
count = int(count_text)
labels = host.split('.')
for i in range(len(labels)):
suffix = '.'.join(labels[i:])
totals[suffix] = totals.get(suffix, 0) + count
return totals