Skip to content

Instantly share code, notes, and snippets.

@arrayed
Created February 25, 2017 05:44
Show Gist options
  • Save arrayed/15470029207f63f7b0416a8182a50497 to your computer and use it in GitHub Desktop.
Save arrayed/15470029207f63f7b0416a8182a50497 to your computer and use it in GitHub Desktop.
interview-cake Temperature Tracker Problem Solution
/* Website: Arrayed.Net
* 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:
*
* insert()—records a new temperature
* get_max()—returns the highest temp we've seen so far
* get_min()—returns the lowest temp we've seen so far
* get_mean()—returns the mean of all temps we've seen so far
* get_mode()—returns the mode of all temps we've seen so far
* Optimize for space and time. Favor speeding up the getter functions (get_max(), get_min(), get_mean(), and get_mode())
* over speeding up the insert() function.
*
* get_mean() should return a float, but the rest of the getter functions 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..110.
*
* If there is more than one mode, return any of the modes.
* https://www.interviewcake.com/question/python/temperature-tracker
*/
#include <iostream>
#include <vector>
using namespace std;
class TempTracker
{
private:
int min;
int max;
int sum;
int nmbTempRecords;
std::vector<int> tempRecords;
int mode;
int mostOccurances;
std::vector<int> occurances;
public:
TempTracker(int size)
{
min = 0;
max = 0;
sum = 0;
mode = 0;
mostOccurances = 0;
nmbTempRecords = 0;
tempRecords.resize(size);
occurances.resize(250);
}
int insert(int temprature)
{
tempRecords[nmbTempRecords++] = temprature;
sum += temprature;
if (temprature < min)
min = temprature;
if (temprature > max)
max = temprature;
occurances[temprature]++;
if (occurances[temprature] > mostOccurances)
{
mostOccurances = occurances[temprature];
mode = temprature;
}
}
int get_max()
{
return max;
}
int get_min()
{
return min;
}
int get_mean()
{
return (sum/nmbTempRecords);
}
int get_mode()
{
return mode;
}
};
int main()
{
TempTracker logtemp(500);
logtemp.insert(5);
logtemp.insert(7);
logtemp.insert(5);
logtemp.insert(8);
logtemp.insert(8);
logtemp.insert(8);
cout << logtemp.get_mode() << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment