Bloomberg L.P. Interview Question: You have a given set of price... | Glassdoor.co.uk

Interview Question

Software Engineer Industrial Placement Interview London, England

You have a given set of prices in an Array, find at which

  price you should buy and at which price you should sell so that you maximize the gain
Answer

Interview Answer

3 Answers

0

The brute-force solution is to compare each price to those further on and to return the greatest gain.
Otherwise I tried a linear solution but I was not sure about the solution.

Interview Candidate on 8 Dec 2014
1

The idea for the linear solution is, that when the next price is lower than current minimum, you should sell all for the previous maximum and buy for the new minimum. It is actually similar to looking for maximum difference where the min is predecessor of max.

Code for the linear solution:

void sell_points(const vector& data)
{
    cout data[max])
        {
            max = i;
        }
    }

    // last run
    if (data[min] < data[max] && max != last_max)
    {
        cout << "Buy:\t#" << min << " (price: " << data[min] << ")" << endl;
        cout << "Sell:\t#" << max << " (price: " << data[max] << ")" << endl;
    }
}

Anonymous on 10 Dec 2014
0

(the last_max check is actually not necessary, in the code I forgot to set it appropriately but it is not needed anyway)

Anonymous on 10 Dec 2014

Add Answers or Comments

To comment on this, Sign In or Sign Up.