N/A Interview Question
Software Engineer / DevelopersCountry: United States
Here is an answer in O(n). Please feel free to comment.
static void Main(string[] args)
{
var inputs = new[] {
new [] {100, 2 },
new [] {100, 1 },
new [] {110, 10 },
new [] {200, 4 },
new [] {1000, 3 },
new [] {1200, 8 }
};
int range = 50;
int value = HighestInterval(inputs, range);
}
private static int HighestInterval(int[][] inputs, int range)
{
int start = 0;
int end = 0;
int total = 0;
int maxIndex = 0;
int maxTotal = 0;
int size = inputs.GetLength(0);
do
{
while (inputs[end][0] - inputs[start][0] > range){
total-=inputs[start][1];
start++;
}
total += inputs[end][1];
if (maxTotal < total){
maxIndex = start;
maxTotal = total;
}
end++;
}while (end < size);
return inputs[maxIndex][0];
}
}
Can you explain the question a bit more?
- HrickenFamster September 26, 2014highestInterval( 50 ):
What is the interval between? What is the base/starting value?
Assuming starting val is 100(minimum of all entries):
So we'relooking for val between 100-150:
Answer should be: 110, its count is 10