Loading
Loading
A ride-hailing app shows drivers the median trip fare so far today, updated after every completed trip. Fares are recorded as whole numbers, and refunds can make them negative.
For each prefix of fares, compute the median and report it doubled, so the answer is always a whole number: for an odd count, twice the middle value; for an even count, the sum of the two middle values. Return one entry per fare.
fares = [5, 15, 1, 3]low = [5], high = []median 5 → 10low = [5], high = [15]median (5 + 15) / 2 → 20low = [1, 5], high = [15]median 5 → 10low = [1, 3], high = [5, 15]5 moved up to rebalance; median (3 + 5) / 2 → 8[10, 20, 10, 8]Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(n log n) time · O(n) space
Input. Run your code to watch it work on this input, one line at a time.
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
The medians are 5, 10, 5 and 4, reported doubled.
Example 2
The medians are 4, 1 and 4, reported doubled.