Write an average-income function
Company: Microsoft
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in data manipulation and basic statistical aggregation, particularly handling missing values and computing averages in Python. Commonly asked in technical interviews for Data Scientist roles within the Coding & Algorithms domain, it gauges practical application and robustness in data cleaning and edge-case handling rather than purely conceptual theory.
Constraints
- 0 <= len(records) <= 100000
- Each record is a dictionary with keys `name` and `income`
- `income` is either an int, a float, or `None`
Examples
Input: [{"name": "a", "income": 100}, {"name": "b", "income": None}, {"name": "a", "income": 200}]
Expected Output: 150.0
Explanation: Only 100 and 200 are included, so the average is (100 + 200) / 2 = 150.0.
Input: [{"name": "x", "income": None}, {"name": "y", "income": None}]
Expected Output: None
Explanation: There are no valid income values, so the function should return None.
Input: []
Expected Output: None
Explanation: An empty list contains no valid income values.
Input: [{"name": "u", "income": -50}, {"name": "v", "income": 150}, {"name": "w", "income": 50}]
Expected Output: 50.0
Explanation: The average is (-50 + 150 + 50) / 3 = 50.0.
Input: [{"name": "solo", "income": 0}]
Expected Output: 0.0
Explanation: A single valid income of 0 gives an average of 0.0.
Hints
- Keep two running values: the sum of valid incomes and the count of valid records.