I came across an interesting puzzle to solve via interviewcake:
Thinking through the issue leads me to the following thought process:Suppose we could access yesterday's stock prices as an array, where:
- The values are the price in dollars of Apple stock.
- A higher index indicates a later time.
So if the stock cost $500 at 10:30am and $550 at 11:00am, then:
stockPricesYesterday[60] = 500;
Write an efficient function that takes stockPricesYesterday and returns the best profit I could have made from 1 purchase and 1 sale of 1 Apple stock yesterday.
For example:
int[] stockPricesYesterday = new int[]{10, 7, 5, 8, 11, 9}; getMaxProfit(stockPricesYesterday); // returns 6 (buying for $5 and selling for $11)
No "shorting"—you must buy before you sell. You may not buy and sell in the same time step (at least 1 minute must pass).
And here's how that looks in code:
Feel free to share your solution in the comments below. I'd love to see how you would solve this problem.
Last week I had a bit of free time and decided to see how difficult it would be to write a Texas Hold'em poker simulation in Groovy. My goal wasn't...
One of the handiest features of Grails is the "flash" scope. The flash scope is "A temporary storage map that stores objects within the session for...
One of the cooler things about the Spark Java framework is built in websocket support thanks to the embedded Jetty server. I've long been fascinated...