Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1619: Mean of Array After Removing Some Elements

In this guide, we solve Leetcode #1619 Mean of Array After Removing Some Elements in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

Given an integer array arr, return the mean of the remaining integers after removing the smallest 5% and the largest 5% of the elements. Answers within 10-5 of the actual answer will be considered accepted.

Quick Facts

  • Difficulty: Easy
  • Premium: No
  • Tags: Array, Sorting

Intuition

Sorting reveals structure that is hard to see in the original order.

Once sorted, a linear scan is usually enough to compute the answer.

Approach

Sort the data and sweep through it while maintaining a small state.

This keeps the logic straightforward and reliable.

Steps:

  • Sort the data.
  • Scan in order while maintaining state.
  • Update the best answer.

Example

Input: arr = [1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3] Output: 2.00000 Explanation: After erasing the minimum and the maximum values of this array, all elements are equal to 2, so the mean is 2.

Python Solution

class Solution: def trimMean(self, arr: List[int]) -> float: n = len(arr) start, end = int(n * 0.05), int(n * 0.95) arr.sort() t = arr[start:end] return round(sum(t) / len(t), 5)

Complexity

The time complexity is O(n log n). The space complexity is O(1) to O(n).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy