cswork / libraryOpen practice workspace

Coding questions · Thumbtack

Implement min, mean, median robustly

This question evaluates implementation skills in basic descriptive statistics (min, mean, median), numerical stability in online aggregation methods, robust handling of edge cases like None/NaN and extreme numeric ranges, and algorithmic complexity reasoning in the Coding & Algorithms domain for a Data Scientist role.

Implement three functions in Python without using numpy/pandas: (1) my_min(nums) returning the minimum in O(n) time and O(1) space; (2) my_mean(nums) using a numerically stable online method (e.g., Kahan or Welford) and explaining why naive summation can be inaccurate; (3) my_median(nums) for both odd and even lengths. First provide an O(n log n) solution using sorting, then implement an O(n) expected-time solution using quickselect; discuss behavior for empty lists, presence of None/NaN, and very large integers/floats; analyze time/space complexity and worst-case pitfalls.

Minimum of a Numeric List

Return the minimum value, or None for empty input.

Examples

Example 1

Input: ([3,1,2],)
Output: 1

Explanation: Minimum.

Example 2

Input: ([],)
Output: None

Explanation: Empty list.

Hints

  1. Use deterministic tie-breaking for prompts with multiple valid outputs.
  2. For design-style APIs, simulate operations with explicit inputs.

Numerically Stable Mean

Return the mean using an online update, rounded to 6 decimals.

Examples

Example 1

Input: ([1,2,3],)
Output: 2.0

Explanation: Mean 2.

Example 2

Input: ([],)
Output: None

Explanation: Empty list.

Hints

  1. Use deterministic tie-breaking for prompts with multiple valid outputs.
  2. For design-style APIs, simulate operations with explicit inputs.

Median of a Numeric List

Return the median after sorting, or None for empty input.

Examples

Example 1

Input: ([3,1,2],)
Output: 2

Explanation: Odd length.

Example 2

Input: ([4,1,2,3],)
Output: 2.5

Explanation: Even length.

Hints

  1. Use deterministic tie-breaking for prompts with multiple valid outputs.
  2. For design-style APIs, simulate operations with explicit inputs.