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.
You're in!
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:
For example:
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.
Log in or sign up with one click to get immediate access to free mock interview questions
We'll never post on your wall or message your friends.
Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?
Log in or sign up with one click to get immediate access to free mock interview questions
We'll never post on your wall or message your friends.
Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?
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.
Log in or sign up with one click to get immediate access to free mock interview questions
We'll never post on your wall or message your friends.
Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?
Reset editor