Compute average unique pins per user
Company: Pinterest
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to process collections and compute aggregated metrics, including per-user deduplication of items, handling of missing/null entries, numeric averaging and rounding.
Constraints
- 0 <= number of users
- Each dictionary value is either None or a list that may contain integers and None values
- Count uniqueness per user, not across all users
- Target time complexity: O(total_items), where total_items is the total number of entries across all user lists
- Use only a temporary set per user to track uniqueness
Examples
Input: ({},)
Expected Output: 0.0
Explanation: There are no users, so the average unique pin count is 0.0.
Input: ({'u1': [], 'u2': [None, None], 'u3': None},)
Expected Output: 0.0
Explanation: Each user has 0 valid unique pins, so the average is (0 + 0 + 0) / 3 = 0.0.
Input: ({'user1': [1, 2, 2], 'user2': [1, 2, 3]},)
Expected Output: 2.5
Explanation: user1 has 2 unique pins: {1, 2}. user2 has 3 unique pins: {1, 2, 3}. Average = (2 + 3) / 2 = 2.5.
Input: ({'a': [1, None, 1, 2], 'b': [2, 2, None, 3, 3, 4], 'c': []},)
Expected Output: 1.67
Explanation: a has 2 unique valid pins: {1, 2}. b has 3: {2, 3, 4}. c has 0. Average = (2 + 3 + 0) / 3 = 1.666..., which rounds to 1.67.
Input: ({'solo': [None, 5, 5, 5]},)
Expected Output: 1.0
Explanation: The only valid unique pin is 5, so the average is 1 / 1 = 1.0.
Hints
- A set is useful for counting distinct pin IDs for one user while ignoring duplicates.
- Make sure you divide by the total number of users in the dictionary, even if some users have empty lists or only None values.