Python interview question: find the bugs in a median function

EasyCode review

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 free
def 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

Fix it free

Fix the function — hidden tests check it in your browser. No sign-up needed.

Skills it tests

More interview questions

All questions