You only have free questions left (including this one).

But it doesn't have to end here! Sign up for the 7-day coding interview crash course and you'll get a free Interview Cake problem every week.

Your cupcake food truck's app tells hungry customers how long they'll probably wait for their order.

At first, the app showed the mean wait time. Then somebody ordered 200 cupcakes for an office party. Say the last seven orders took 6, 9, 2, 4, 11, 3, and 95 minutes. The mean of those is about 18.6 minutes, even though six of the seven orders were ready in 11 minutes or less. Customers took one look at "18.6 minutes" and walked to the taco truck next door.

So you're switching to the median: the middle wait time when all the wait times are lined up in order. For those seven orders, the median is 6 minutes, and one giant catering order can't drag it around.

Write a class MedianTracker with these methods:

  1. insert—records the wait time of an order that was just picked up
  2. getMedian—returns the median of all the wait times recorded so far

For example:

MedianTracker tracker = new MedianTracker(); tracker.insert(6); tracker.getMedian(); // returns 6.0 tracker.insert(9); tracker.getMedian(); // returns 7.5 (halfway between 6 and 9) tracker.insert(2); tracker.getMedian(); // returns 6.0 (the middle of 2, 6, 9)

Orders get picked up all day long, and the app asks for the median every time someone opens it. Make getMedian as fast as you can, while keeping insert fast too.

Wait times are whole numbers of minutes, so insert takes an integer. If you've recorded an even number of wait times, there's no single middle one, so the median is the average of the two middle wait times. getMedian should return a double.

You can assume getMedian won't be called until at least one wait time has been inserted.

We can get O(log n) time for insert and O(1) time for getMedian.

With an even number of wait times, the median might not be a whole number. The median of 6 and 9 is 7.5. Careful with integer division: if both numbers are integers, 15 / 2 gives you 7, not 7.5.

Lots of orders take exactly the same amount of time. Make sure your code still works when the same wait time shows up again and again.

Start your free trial!

Log in or sign up with one click to get immediate access to free mock interview questions

Where do I enter my password?

Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?

  1. It's easy and quick. No "reset password" flow. No password to forget.
  2. It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
  3. It makes it harder for one person to share a paid Interview Cake account with multiple people.

Start your free trial!

Log in or sign up with one click to get immediate access to free mock interview questions

Where do I enter my password?

Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?

  1. It's easy and quick. No "reset password" flow. No password to forget.
  2. It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
  3. It makes it harder for one person to share a paid Interview Cake account with multiple people.

O(log n) time for insert and O(1) time for getMedian, where n is the number of wait times we've recorded.

Each insert does one push onto a heap, plus at most one pop and one push to rebalance. Each of those steps takes O(log n) time, and doing a constant number of them is still O(log n). getMedian only peeks at the tops of the heaps, which takes constant time.

We use O(n) space, since we store every wait time. We can't get around that one: any wait time we've seen could end up being the median later, depending on what comes in next, so we can't throw any of them away.

  1. Customers care most about how things are going right now. How would you track the median of just the last 100 wait times? Now old wait times have to leave the heaps, and they usually won't be sitting on top.
  2. The app also wants to show a "worst case" estimate: the 90th percentile wait time (90% of orders are ready by then). How could you adapt the two heaps to track that instead of the median?
  3. Suppose every wait time is a whole number of minutes from 0 to 120. Could you use that to make insert O(1), while keeping getMedian O(1) too? (Top Scores uses a similar trick.)

Start your free trial!

Log in or sign up with one click to get immediate access to free mock interview questions

Where do I enter my password?

Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?

  1. It's easy and quick. No "reset password" flow. No password to forget.
  2. It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
  3. It makes it harder for one person to share a paid Interview Cake account with multiple people.

Reset editor

. . .