"Subdomain Visit Count" is a medium-difficulty string and data structure problem. You are given a list of "count-paired domains" where each entry consists of a visit count and a domain name (e.g., "9001 discuss.google.com"). You need to calculate the total number of visits for every subdomain mentioned. For "discuss.google.com," the subdomains are "discuss.google.com," "google.com," and "com." The output should be a list of the counts and their corresponding subdomains in any order.
Companies like Microsoft, Roblox, and Google use this question to evaluate a candidate's string parsing and hash map usage. It's a very practical problem that mimics real-world log processing. It tests whether you can correctly split strings, iterate through the different levels of a domain hierarchy, and aggregate data efficiently. It's also a good test of how you handle input strings with different numbers of segments.
The pattern for this problem is String Parsing and Hash Table Aggregation. For each input string:
Input: ["50 info.example.org"]
A common mistake is not correctly identifying all levels of the subdomain (e.g., forgetting the top-level domain like ".com"). Another mistake is inefficiently recreating strings in a loop, which can lead to higher time complexity in languages where strings are immutable. Using a complex recursive solution is also usually overkill for this problem; a simple iterative approach using split and substring is much cleaner.
For the Subdomain Visit Count interview question, focus on writing clean and modular code. Mention how you handle the string splitting—using a built-in split function is usually fine. Be careful with space complexity; the number of entries in your hash map depends on the number of unique subdomains, which is a good point to discuss with your interviewer.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Sender With Largest Word Count | Medium | Solve | |
| Find the Most Common Response | Medium | Solve | |
| Number of Pairs of Strings With Concatenation Equal to Target | Medium | Solve | |
| Most Common Word | Easy | Solve | |
| Find Words That Can Be Formed by Characters | Easy | Solve |