Community Problem
Subdomain Visit Count
Difficulty: Medium
Aggregate visit counts across every subdomain prefix from a list of count-paired domain strings.
Subdomain Visit Count
Aggregate visit counts across every subdomain prefix from a list of count-paired domain strings.
By @jamesmurphy
May 9, 2026
·
Updated May 18, 2026
561 views
16
4.4 (10)
Hit this one on a takehome screening round at an analytics startup, framed as a log aggregation warmup. The official catalog has Group Anagrams and Top K Frequent for the hash-table family but skipped the every-suffix-counts variation, which is the prettier teaching example for "build the key on the fly while iterating" and is borderline indistinguishable from real production traffic-rollup code.
Subdomain Visit Count
A website domain like discuss.leetcode.com consists of several subdomains. At the top level we have com, at the next level we have leetcode.com, and at the lowest level we have discuss.leetcode.com. When we visit discuss.leetcode.com, we are also implicitly visiting its parent domains leetcode.com and com.
A count-paired domain is a string of the form "<count> <domain>" where count is a positive integer representing the number of visits the domain received. For example, "9001 discuss.leetcode.com" means there were 9001 visits to discuss.leetcode.com.
Given an array cpdomains of count-paired domains, return an array of count-paired domains for each subdomain in the input, summed across all inputs. The output may be returned in any order.
Examples
Example 1:
- Input:
cpdomains = ["9001 discuss.leetcode.com"] - Output:
["9001 leetcode.com", "9001 discuss.leetcode.com", "9001 com"](any order) - Explanation: Visiting
discuss.leetcode.comonce with weight 9001 implicitly visitsleetcode.comandcomwith the same weight.
Example 2:
- Input:
cpdomains = ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"] - Output:
["901 mail.com", "50 yahoo.com", "900 google.mail.com", "5 wiki.org", "5 org", "1 intel.mail.com", "951 com"] - Explanation:
mail.comaggregates the 900 fromgoogle.mail.comand the 1 fromintel.mail.com(total 901).comaggregates 900 + 50 + 1 = 951.
Example 3:
- Input:
cpdomains = ["5 a.b.c.d"] - Output:
["5 a.b.c.d", "5 b.c.d", "5 c.d", "5 d"] - Explanation: Every suffix starting at a domain boundary inherits the count.
Constraints
1 <= cpdomains.length <= 1001 <= cpdomains[i].length <= 100cpdomains[i]follows the format"<count> <domain>"wherecountis a positive integer with at most nine digits anddomainconsists of lowercase letters and dots.- The number of dots in a domain is between
0and3.
Follow-up
The count-paired strings can be parsed once into (count, domain) and then split on dots, but you can also keep moving the dot index forward and slice in place. Both are O(total characters) and the second avoids the intermediate split array. Either is fine for the given constraints.
Solution
Hints
Solution Walkthrough
Approach: Hash Map Keyed by Every Subdomain Suffix (O(N * L) time, O(unique suffixes) space)
The natural data structure is a frequency map keyed by domain string. For each input "<count> <domain>", parse the count and the domain, then walk the dots of the domain to enumerate every suffix. Add the count to the map at every suffix. Finally, format the map back into the requested "<count> <domain>" strings. Where N is the number of inputs and L is the average string length, every dot-walk is O(L), so the total cost is O(N * L).
Algorithm
- Initialize an empty map
counts. - For each
cpincpdomains:- Find the first space; everything before is the integer count, everything after is the domain.
- Add
counttocounts[domain](the full domain itself counts). - For each dot index
jindomain, addcounttocounts[domain[j + 1:]].
- Build the result list as
"<value> <key>"for every entry incounts.
Why It Works
The parent-of relation on subdomains is exactly the suffix-after-a-dot relation. Every visit to discuss.leetcode.com is one visit to itself plus one visit to each suffix starting after a dot, which is exactly what the map writes do. Different inputs that share a suffix accumulate at that suffix's key, which is precisely the aggregation the problem asks for.
Complexity
Metric Value
Time O(N * L), N inputs, average string length L
Space O(unique suffixes), at most 4 per input under the constraintsPitfalls
for...ofon a Map in Sandpack. Sandpack's TS transpile targets ES5, which silently drops Map and Set iteration. Use a plain object plusObject.keys()(the solution above does this) or stick to the practice runner which supports modern targets.split('.')and forgetting to rejoin. The clean Python one-liner isfor j in range(len(parts)): counts['.'.join(parts[j:])] += count. The slice-based version above avoids allocating the parts array but the join-based version is also fine and reads slightly more clearly.- Integer parsing with
parseInt(s)(no radix) in JS. Always pass the radix10. The catch is mostly historical (octal interpretation for leading zeros) but linters still flag it. - Output ordering. The problem accepts any order, so the tests above check the (key, value) pairs in a map rather than the array order. Tying tests to an iteration order would create flakiness across language and engine.
Solution Code
