Given a list of historic stock prices,
write a function that tells when you should have bought and sold to make the most profit.
You can only buy and sell once.
Example input:
[
{date: '2024-02-28', price: 130.0},
{date: '2024-03-01', price: 150.0},
{date: '2024-03-02', price: 132.0},
{date: '2024-03-03', price: 147.0},
{date: '2024-03-04', price: 110.0},
{date: '2024-03-05', price: 199.0},
{date: '2024-03-06', price: 117.0},
{date: '2024-03-07', price: 100.0}
]
Goal:
Return the best buy date and best sell date to maximize profit.
One pass: track the lowest price so far, and at each step compute potential profit price - min_price.
Keep the max profit and corresponding buy/sell dates.
The VOprep team has long accompanied candidates through various major company OAs and VOs, including Google, Amazon, Citadel, SIG, providing real-time voice assistance, remote practice, and interview pacing reminders to help you stay smooth during critical moments. If you are preparing for these companies, you can check out our customized support plans—from coding interviews to system design, we offer full guidance to help you succeed.