PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • easy
  • Microsoft
  • Coding & Algorithms
  • Data Scientist

Write an average-income function

Company: Microsoft

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

Given a Python list of dictionaries such as: `records = [{"name": "a", "income": 100}, {"name": "b", "income": None}, {"name": "a", "income": 200}]` Write a function that returns the average of all non-missing `income` values. Requirements: - Ignore any record where `income` is `None`. - The `name` field does not affect the calculation. - If there are no valid income values, return `None`.

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.

Given a Python list of dictionaries, return the average of all non-missing `income` values. Ignore any record where `income` is `None`. The `name` field does not affect the calculation. If there are no valid income values, return `None`.

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

  1. Keep two running values: the sum of valid incomes and the count of valid records.
  2. After processing all records, check whether the count is zero before dividing.
Last updated: Apr 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)