Monday, April 14, 2014

Suggest the selling time and buying time of a share based on stock price prediction

Problem

You have an API to predict stock values of a particular share,
The API is
StockPrediction predict(int stockid);

where
class StockPrediction{
   Date time:
   float value;
}

using this API develop another API which will suggest the best selling time and buying time of a share (you have to call the predict API N number of times and from the StockPredictions provide the best buy time and sell time for the stock)

Your API can be
BestTiming getBestTiming(int stockid);

where
class BestTiming{
   StockPrediction bestselltime:
   StockPrediction bestbuytime:
}

Example
Input

stock value -  10   |   12   |    7   |    8    |  24  |  35  |   1  |   9
time        -  9am  |  9.30  |   9.45 |   10am  | 11am | 12am |  3am |  4am


Output: buy the stock at 7 rs at 9.45 and sell it for 35 rupees at 12am

(hint: go for the best solution which uses only three variable to get this result)

Solution

We have discussed the similar problem here.
Fill array b with entries s.t. b[0] = a[1]-a[0] .... b[n-1] = a[n]-a[n-1].
Now apply Kadane's Sub sequence sum algorithm on B and Find the indices.
Time O(n), Space O(n).

0 comments:

Post a Comment