Mode Calculation

PySpark data engineering interview problem. Difficulty: intermediate. Pattern: Aggregation. About 16 minutes. Part of the Pro drill bank.

Most frequent amount value. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

The mode is the value that appears the most times. In a list of 10, 20, 10, 30, 10 and 20, the value 10 appears 3 times, more than any other. Return 1 row with the column mode_amount, the most common amount in df. If 2 values appear the same number of times, return the smaller one. The editor creates the table df and ends with result = df. Change the last line to build your answer. Count how many times each amount appears, then pick the amount with the highest count.

Requirements

  • Count how many times each amount appears.
  • Pick the amount with the highest count.
  • When counts tie, pick the smaller amount.
  • Return 1 row with mode_amount, and store it in result.

Constraints

  • The table has 1 column, amount, with decimal values.
  • The tie rule is part of the order: highest count first, then smallest amount.
  • Return exactly 1 row.

Examples

Input: repeated amounts Output: mode_amount 10.0 10 appears 3 times.

Topics: lakebench, pyspark, mode, groupBy.

More PySpark interview questions · All interview problems · Learn data engineering

intermediate

Mode Calculation

Interview-style drill: Most frequent amount value.

Return 1 row with `mode_amount` equal to the most frequent `amount` (ties → smaller amount). Assign `result`.