Python interview question: find the bugs in a median function
Problem
revenue is a store’s daily revenue. Return the indices of days with revenue strictly above the median, in ascending order; with an even number of days, the median is the mean of the two middle values. An empty list gives [].
Function to fix
Fix it freedef days_above_median(revenue: list[int]) -> list[int]:
# sort the revenue and take the middle element — that's the median
revenue.sort()
median = revenue[len(revenue) // 2]
return [day for day, amount in enumerate(revenue) if amount > median]
Example
revenue = [120, 340, 90, 410, 260] → [1, 3]260 is the median itself, not above itrevenue = [300, 150, 450, 200] → [0, 2]an even count: the median is 250, the mean of 200 and 300
Fix it free
Fix the function — hidden tests check it in your browser. No sign-up needed.