I just got this from a company for a interview test and I completed it with ease but they said that my functions were in o(n). Heres the questions
Write a class IntegerTracker with these methods:
track(int) - Receives an integer for tracking.
get_max() - Returns the max (int) of all integers seen so far.
get_min() - Returns the min (int) of all integers seen so far.
get_mean() - Returns the mean (float) of all integers seen so far.
get_mode() - Returns the mode (int) of all integers seen so far.
Ensure each method, including track, runs in constant time (O(1) time complexity).
This is how I completed it
- (instancetype)init{
if(self == [super init]){
self.numbers = [[NSMutableArray alloc]init];
}
return self;
}
- (void)trackInt:(int)number{
[self.numbers addObject:[NSNumber numberWithInt:number]];
}
- (int)getMax{
NSNumber *max = [self.numbers valueForKeyPath:@"@max.self"];
return [max intValue];
}
- (int)getMin{
NSNumber *min = [self.numbers valueForKeyPath:@"@min.self"];
return [min intValue];
}
- (float)getMean{
NSNumber *average = [self.numbers valueForKeyPath:@"@avg.self"];
return [average floatValue];
}
- (int)getMode{
int maxCount = 0;
int value = 0;
NSMutableDictionary *mode = [[NSMutableDictionary alloc]init];
for(NSNumber *n in self.numbers){
int currentCount = [[mode objectForKey:n.stringValue]intValue];
currentCount++;
[mode setObject:@(currentCount) forKey:n.stringValue];
if(maxCount < currentCount){
maxCount = currentCount;
value = [n intValue];
}
}
return value;
}
Can someone show me how I am supposed to complete this in O(1). I already got passed up cause of this so don't think your giving me an answer for the interview. I'm not going to work there. I just want to see how i'm supposed to figure this out without iterating through the array.