Second largest order total

Python data engineering interview problem. Difficulty: beginner. Pattern: Sorting. About 10 minutes. Free to practice.

Finance dumps daily order totals into a list. Reporting wants the second-highest distinct amount so they can compare the runner-up basket to the winner, even when many orders share the top value.

An order report needs second place, but 2 orders with the same total should not take both places. Return the second largest distinct value in the list. If there are fewer than two distinct values, return None. Write second_largest(totals). In [1, 5, 5, 3] the distinct totals are 5, 3 and 1, so second place is 3. In [7] there is only one value, so the answer is None. The starter file already has the function name and its print lines. Keep them and fill in the body.

Requirements

  • Ignore repeated values.
  • Return the second largest of the distinct values.
  • Return None when there are fewer than two distinct values.

Constraints

  • totals is a list of numbers and can be empty.
  • The input list should not be changed.

Examples

Input: second_largest([1, 5, 5, 3]) Output: 3 This input follows the stated rules and produces this output.

Topics: lakebench, python, lists.

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

beginner

Second largest order total

Interview-style drill: Given a list of order totals, return the second-largest distinct value.

Return the second-largest distinct number from a list of order totals.