Quick Overview

This question evaluates data manipulation and aggregation skills, focusing on handling duplicate values within collections and computing numerical summaries of list data.

Compute Averages of Unique Numbers in Dictionary Lists

Company: TikTok

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

##### 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.

Quick Answer: This question evaluates data manipulation and aggregation skills, focusing on handling duplicate values within collections and computing numerical summaries of list data.

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

Hints

  1. Use a set to deduplicate each list before averaging.
  2. Compute the mean as sum(unique)/len(unique); if unique is empty, use 0.0.
  3. Note that 1 and 1.0 are treated as the same value when deduplicating.

Loading coding console...