Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java Code Challenge: Play the Stock Market

DZone's Guide to

Java Code Challenge: Play the Stock Market

The latest code challenge from Sam Atkinson: Playing the stock market.

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

Java Code Challenge is a new regular segment taking the best challenge from Reddit's dailyprogrammer. Things are a little different here as we're focused on Java. A working solution is not enough; we're looking for the cleanest Java code with tests. Third party libraries are welcome but if you can do it without them, it will be easier for others to comprehend.

If you can fit your solution in the comments, then go for it, but preferably put your answer in GitHub and link in the comments. Next week, we'll be sharing the best solutions and sharing the best code practices we see! This challenge should be a good opportunity to flex your Java 8 muscles.

Description

Let's assume I'm playing the stock market—buy low, sell high. I'm a day trader, so I want to get in and out of a stock before the day is done, and I want to time my trades so that I make the biggest gain possible.

The market has a rule that won't let me buy and sell in a pair of ticks—I have to wait for at least one tick to go buy. And obviously I can't buy in the future and sell in the past.

So, given a list of stock price ticks for the day, can you tell me what trades I should make to maximize my gain within the constraints of the market? Remember—buy low, sell high, and you can't sell before you buy.

Input Description

You'll be given a list of stock prices as a space separated list of two decimal floats (dollars and cents), listed in chronological order. Example:

19.35 19.30 18.88 18.93 18.95 19.03 19.00 18.97 18.97 18.98

Output Description

Your program should emit the two trades in chronological order—what you think I should buy at and sell at. Example:

18.88 19.03

Challenge Input

9.20 8.03 10.02 8.08 8.14 8.10 8.31 8.28 8.35 8.34 8.39 8.45 8.38 8.38 8.32 8.36 8.28 8.28 8.38 8.48 8.49 8.54 8.73 8.72 8.76 8.74 8.87 8.82 8.81 8.82 8.85 8.85 8.86 8.63 8.70 8.68 8.72 8.77 8.69 8.65 8.70 8.98 8.98 8.87 8.71 9.17 9.34 9.28 8.98 9.02 9.16 9.15 9.07 9.14 9.13 9.10 9.16 9.06 9.10 9.15 9.11 8.72 8.86 8.83 8.70 8.69 8.73 8.73 8.67 8.70 8.69 8.81 8.82 8.83 8.91 8.80 8.97 8.86 8.81 8.87 8.82 8.78 8.82 8.77 8.54 8.32 8.33 8.32 8.51 8.53 8.52 8.41 8.55 8.31 8.38 8.34 8.34 8.19 8.17 8.16

Challenge Output

8.03 9.34

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:
code challenge ,java 8

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}