FastPrepRunning Delivery Time Medians

Running Delivery Time Medians

Amazon logoAmazon● MediumFULLTIMEPHONE SCREENONSITE INTERVIEW

Problem statement

Given an array deliveryTimes, process the values from left to right. After each new delivery time arrives, output the median of all delivery times seen so far.

When the number of seen values is even, use the lower median, meaning the larger value in the lower half after sorting.

Return an array containing the median after each insertion.

Function

runningDeliveryMedians(deliveryTimes: int[]) → int[]

Examples

Example 1

deliveryTimes = [5,17,100,11]return = [5,5,17,11]

The sorted prefixes are [5], [5,17], [5,17,100], and [5,11,17,100]. Their lower medians are 5, 5, 17, and 11.

Constraints

  • deliveryTimes are processed from left to right.
  • After each new value is inserted, record the median of all values seen so far.
  • When the number of seen values is even, use the lower median: the larger value in the lower half after sorting.
  • Return one median for each insertion.

More Amazon problems

See Amazon hiring insights
Saved
public int[] runningDeliveryMedians(int[] deliveryTimes) {
  // write your code here
}
deliveryTimes[5,17,100,11]
expected[5,5,17,11]
Checking account…