Coding questions · TikTok
Compute Averages of Unique Numbers in Dictionary Lists
This question evaluates data manipulation and aggregation skills, focusing on handling duplicate values within collections and computing numerical summaries of list data.
Scenario
Python tech screen: given a dictionary mapping keys to numeric lists, e.g., {'a':[1,2,1],'b':[1,2,3]}, compute the average of each list after removing duplicates.
Question
Write Python code that takes any such dictionary and returns a new dictionary whose values are the average of the unique numbers in each original list.
Hints
Deduplicate each list (set or list(dict.fromkeys())), then take the mean.
Average of Unique Numbers Per Key
Given a dictionary mapping strings to lists of numbers (integers or floats), return a new dictionary mapping each key to the arithmetic mean of the unique numbers in its list. Duplicates within a list are ignored. If a list is empty, return 0.0 for that key.
Constraints
- 0 <= number of keys <= 10^4
- 0 <= length of each list <= 10^5
- Sum of lengths across all lists <= 2 * 10^5
- Values are integers or floats in the range [-1e9, 1e9]
- Integers and floats equal by value (e.g., 1 and 1.0) are considered duplicates
- If a list is empty, the average for that key is 0.0
Examples
Example 1
Input: data = {'a':[1,2,1],'b':[1,2,3]}
Output: {'a': 1.5, 'b': 2.0}
Explanation: For 'a', unique {1,2} -> (1+2)/2=1.5; for 'b', unique {1,2,3} -> (1+2+3)/3=2.0.
Example 2
Input: data = {'x':[4,4,4,4], 'y':[1.0,2,2.0,3.0,3]}
Output: {'x': 4.0, 'y': 2.0}
Explanation: Unique values: x->{4}, avg 4.0; y->{1.0,2,3.0}, avg (1+2+3)/3=2.0.
Hints
- Use a set to deduplicate each list before averaging.
- Compute the mean as sum(unique)/len(unique); if unique is empty, use 0.0.
- Note that 1 and 1.0 are treated as the same value when deduplicating.
Approach
For each key, convert its list to a set to remove duplicates, then compute the arithmetic mean of the unique values. If the list is empty, define the average as 0.0. Using a set provides O(n) processing per list and treats equal integers and floats as duplicates.
- Time complexity: O(N) where N is the total number of elements across all lists
- Space complexity: O(U + K) where U is the maximum number of unique values in any one list and K is the number of keys