InMobi Interview Question
Country: India
Interview Type: In-Person
List<float> values;
List<DateTime> times
int startIndex=0;
int endIndex;
float maxValue=0;
BestTiming answer = new BestTiming();
for(int i=1;i<values.Count;i++)
{
if(values[i-1] > values[i])
{
endIndex = i-1;
startIndex = i;
}
if(maxValue < (values[i] - values[startIndex]))
{
maxValue = values[i] - values[startIndex];
answer.bestbuyTime = startIndex;
answer.bestSellTime = i;
}
}
return answer;
List<float> values;
List<DateTime> times
int startIndex=0;
int endIndex;
float maxValue=0;
BestTiming answer = new BestTiming();
for(int i=1;i<values.Count;i++)
{
if(values[i-1] > values[i])
{
endIndex = i-1;
startIndex = i;
}
if(maxValue < (values[i] - values[startIndex]))
{
maxValue = values[i] - values[startIndex];
answer.bestbuyTime = startIndex;
answer.bestSellTime = i;
}
}
return answer;
List <StockPrediction> stocks;
BestTiming answer = new BestTiming();
int maxIndex, difference,maxDifference;
int min,minIndex;
max=difference=maxDifference=0;
min=Number.Maximum;
for(int i=0;i<stocks.count;i++)
{
if(stocks[i].values<min)
{
min=stocks[i].values;
temporaryMinIndex=i;
}
else if(stocks[i].value>min)
{
difference=stocks[i].values-min;
if(difference>maxDifference)
{
maxIndex=i;
minIndex=temporaryIndex;
}
}
}
answer.bestBuyTime=stocks[minIndex].time;//or just the minIndex so you can get the value as well
answer.bestSellTime=stocks[maxIndex].time;
Fill array b with entries s.t. b[0] = a[1]-a[0] .... b[n-1] = a[n]-a[n-1].
- VillageMonkey May 19, 2012Now apply Kadane's Sub sequence sum algorithm on B and Find the indices.
Time O(n), Space O(n).