KatInterview

Column-wise Gap Filling with Mean, Median or Mode

Arrays & HashingAuto-graded · 22 test cases
MediumAuto-gradedArrays & Hashingarrayssimulationstatistics

Last updated

Fill None cells in a table column by column with the mean, median or smallest-most-frequent value of the observed entries, without mutating the input.

You are given a table as a list of rows. A cell holds a value, or None if it is missing. Write a function that fills every missing cell using a statistic computed from the other cells in the same column, and returns the completed table.

Inputs

  • table: a list of rows, each a list of cells. Every row has the same length as the first row. A cell is either None or a value. For "mean" and "median" the values are numbers. For "mode" the values can be any mutually comparable, hashable type (for example numbers or strings).
  • method: one of "mean", "median", "mode".

Output

A new table of the same shape in which each None has been replaced by its column's replacement value. Cells that were not None stay unchanged. The input table and its rows must not be modified.

Replacement rules (per column)

Let the observed values of a column be all its cells that are not None.

  • "mean": the arithmetic mean of the observed values.
  • "median": the middle observed value after sorting. If the count is even, the average of the two middle values.
  • "mode": the most frequent observed value. If several values tie for the highest frequency, use the smallest of them.
  • If a column has no observed values, its replacement is None, so the column stays entirely missing.

Error and edge-case behavior

  • If table is empty, return [].
  • If method is not one of the three allowed strings, raise ValueError. This is checked lazily: the check happens only when a column that has at least one observed value is processed. A table whose columns are all entirely missing, or whose rows have width 0, therefore returns normally even with an invalid method.
  • Columns are handled independently of each other.

Constraints

  • 0 ≤ number of rows ≤ 10^4
  • 0 ≤ number of columns ≤ 100
  • All rows have the same length.
  • Within one column, observed values are mutually comparable. For "mean" and "median" they are numeric.
  • The returned table must be a fresh structure. Mutating the input is not allowed.

Examples

Example 1

Input: fill_missing_cells([[1, 2], [3, None], [None, 2]], 'mean')

Output: [[1, 2], [3, 2], [2, 2]]

Example 2

Input: fill_missing_cells([[1, None], [5, 8], [9, None], [None, 8]], 'median')

Output: [[1, 8], [5, 8], [9, 8], [5, 8]]

Example 3

Input: fill_missing_cells([['1', None], ['2', '2'], [None, '1'], ['1', None]], 'mode')

Output: [['1', '1'], ['2', '2'], ['1', '1'], ['1', '1']]

Hints

Hint 1

Handle each column separately. First gather the cells in that column that are not None, then derive one replacement value from them.

Hint 2

For the median, sort the observed values and check the parity of their count. For the mode, count occurrences, find the highest count, and take the minimum among the values reaching it.

Hint 3

Copy each row before writing, and remember the order of checks: an empty column yields None without ever looking at the method, while an unknown method only raises once a column with observed data is reached.