You only have 3 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.

You decide to test if your oddly-mathematical heating company is fulfilling its All-Time Max, Min, Mean and Mode Temperature Guarantee™.

Write a class TempTracker with these methods:

  1. insert()—records a new temperature
  2. getMax()—returns the highest temp we've seen so far
  3. getMin()—returns the lowest temp we've seen so far
  4. getMean()—returns the mean

    The mean of a set of values is the average value of all the items in the set.

    mean=sum of all valuesnumber of values\text{mean}=\frac{\text{sum of all values}}{\text{number of values}}
    of all temps we've seen so far
  5. getMode()—returns a mode

    The mode of a set of values is the number which appears the most times.

    For example, in this set:

      1, 3, 6, 3, 1, 3
    

    The number 3 appears the most times, so it's the mode.

    Careful: a set may have multiple modes.

    of all temps we've seen so far

Optimize for space and time. Favor speeding up the getter methods getMax(), getMin(), getMean(), and getMode() over speeding up the insert() method.

getMean() should return a double, but the rest of the getter methods can return integers. Temperatures will all be inserted as integers. We'll record our temperatures in Fahrenheit, so we can assume they'll all be in the range 0..1100..110.

If there is more than one mode, return any of the modes.

Gotchas

We can get O(1)O(1) time for all methods.

We can get away with only using O(1)O(1) additional space. If you're storing each temperature as it comes in, be careful! You might be taking up O(n)O(n) space, where nn is the number of temperatures we insert!

Are you trying to be fancy about returning multiple modes if there's a tie? Good idea, but read the problem statement carefully! Check out that last sentence!

Failing to carefully read or listen to the problem statement is a very common mistake, and it always looks bad. Don't let it happen to you.

Breakdown

Start your free trial!

Log in or sign up with one click to get immediate access to 3 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.

Solution

Start your free trial!

Log in or sign up with one click to get immediate access to 3 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.

Complexity

O(1)O(1) time for each method, and O(1)O(1) space related to input! (Our occurrences vector's size is bounded by our range of possible temps, in this case 0-110)

What We Learned

Start your free trial!

Log in or sign up with one click to get immediate access to 3 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.

Bonus

There's at least one way to use a just-in-time approach, have O(1)O(1) time for each operation, and keep our space cost at O(1)O(1) for nn inserts. How could we do that?

Do you have an answer?

6
7
8
# Implement methods to track the max, min, mean, and mode
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Reset editor

Powered by qualified.io

. . .