Leaf Domain Cumulative Scores
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates a candidate's ability to model hierarchical string data and aggregate values along a tree structure, a common pattern in coding interviews involving tries and suffix-based hierarchies. It tests practical algorithm design skills such as identifying leaf nodes, propagating ancestor sums, and handling edge cases like single-label entries efficiently at scale.
Constraints
- 1 <= number of domains <= 10^5
- Total length of all domain strings <= 10^6 characters
- Domains are lowercase ASCII, one or more non-empty labels separated by single dots, no leading/trailing dots
- Each domain appears at most once
- Scores are integers and may be negative
Examples
Input: ([['com', 20], ['domain.com', 10], ['mail.domain.com', 5], ['test.com', 10], ['user.test.com', 30], ['contact.user.test.com', -5]],)
Expected Output: [['contact.user.test.com', 55], ['mail.domain.com', 35]]
Explanation: Only mail.domain.com and contact.user.test.com are leaves; com/domain.com/test.com/user.test.com are ancestors of others. Scores: 5+10+20=35 and -5+30+10+20=55, sorted lexicographically.
Input: ([['com', 20]],)
Expected Output: [['com', 20]]
Explanation: A single-label domain with no children is itself a leaf; its cumulative score is just its own score.
Input: ([['com', 20], ['a.b.com', 7]],)
Expected Output: [['a.b.com', 27]]
Explanation: com is an ancestor of a.b.com (not a leaf). a.b.com is a leaf; ancestor b.com is absent so contributes 0, but com=20 is present: 7+20=27.
Input: ([['x', 10], ['a.x', -5], ['b.x', 3]],)
Expected Output: [['a.x', 5], ['b.x', 13]]
Explanation: x has two children and is not a leaf. Both a.x and b.x are leaves: -5+10=5 and 3+10=13.
Input: ([['a', 1], ['b.a', 2], ['c.b.a', 3]],)
Expected Output: [['c.b.a', 6]]
Explanation: A straight 3-level chain; only the deepest domain c.b.a is a leaf: 3+2+1=6.
Input: ([['net', -4], ['sub.net', -1]],)
Expected Output: [['sub.net', -5]]
Explanation: All-negative scores: net is an ancestor (not a leaf); sub.net leaf = -1 + -4 = -5.
Hints
- A domain X is a leaf iff no other input domain has X as a proper dot-boundary suffix. First collect every domain that appears as such a suffix — those are exactly the non-leaves.
- The ancestors of a domain are exactly the strings you get by dropping its leading labels one at a time: mail.domain.com -> domain.com -> com.
- Store scores in a hash map keyed by the full domain. A leaf's cumulative score is its own score plus the scores of the ancestor suffixes that are actually present in the map (missing ones contribute nothing).