DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Java Code Challenge: Play the Stock Market

Java Code Challenge: Play the Stock Market

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

Sam Atkinson user avatar by
Sam Atkinson
·
Jul. 17, 16 · Java Zone · Tutorial
Like (5)
Save
Tweet
18.48K Views

Join the DZone community and get the full member experience.

Join For Free

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
Java (programming language) code style

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Blocking Ads on Your Network Using Raspberry Pi 3 + Fedora + Pi-hole
  • Applying Domain-Driven Design Principles to Microservice Architectures
  • Which Backend Frameworks Are Impacting Web App Development Immensely?
  • API Security Weekly: Issue 165

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo