js

Stock Price Tracking

Submitted by daniel on Fri, 04/12/2020 - 03:02

Say we have an array of values that represents the stock price of duckduckgo over time.

e.g.

const stocks = [1, 0, 5, 14,  -1, 10];

How do we write a function that tells us what the biggest margin is? What is the optimal price that we can both buy and sell at? We cannot sell stock before we buy it. 

One approach requires iterating though each number and calculate the delta of every price in the future to ensure that we only make comparisons going forward in time. 

Here is an example

Tags: