# Buy and Sell Stock — Medium coding interview question

Aced (formerly Exponent) publishes a public search API over its indexed pages; the guide is at https://www.aced.io/agents/agent-access.

Source: https://www.aced.io/questions/1731/buy-sell-stock
Source observed: 2026-11-04T22:21:37.184Z
Last checked: 2026-11-04T22:21:37.184+00:00
Indexed: 2026-11-05T20:01:38.682+00:00

---

# Given stock prices for the next n days, how can you maximize your profit by buying or selling one share per day?

**Difficulty:** Medium  
**Category:** Data Structures & Algorithms; Coding

We use this question to practice maximizing stock-trading profit over consecutive daily prices. You may complete any number of transactions, subject to these rules:

- You must buy before you sell.
- You cannot buy and sell on the same day; treating both actions as occurring on one day counts as no transaction.
- Your objective is to maximize total profit.

For example, with prices `1, 2, 3, 4, 5`, buying on day 1 at `1` and selling on day 5 at `5` produces profit `4`.

## Examples

```text
stock_prices = [7, 1, 5, 3, 8, 6]
output: 9
(Buy on day 2 at 1 and sell on day 3 at 5, buy on day 4 at 3 and sell on day 5 at 8)

stock_prices = [1, 2, 3, 4, 5]
output: 4
(Buy on day 1 at 1 and sell on day 5 at 5)

stock_prices = [7, 6, 4, 3, 1]
output: 0
(No transaction is done; max profit = 0)

stock_prices = [1, 10, 2, 3]
output: 10
(Buy on day 1 at 1 and sell on day 2 at 10, buy on day 3 at 2 and sell on day 4 at 3)

stock_prices = [10, 1, 5, 8, 7, 12]
output: 11
(Buy on day 2 at 1 and sell on day 6 at 12)
```

The page includes an interactive Python editor with **Editor**, **Share**, **Output**, **Test Results**, and **Console** panels. Running code requires [signing up](https://www.aced.io/signup?src=lesson). The solution and mock-interview tabs are locked for this question. No community answers have been contributed yet; the Community tab shows `27`.

## Interview metadata

The question is associated with **17 interviews**, with the latest reported occurrence **3 months ago** as observed on **2026-11-04**. The displayed role counts are:

- [Software Engineer](https://www.aced.io/questions?role=swe): 10
- [Data Engineer](https://www.aced.io/questions?role=data-engineer): 4
- [Machine Learning Engineer](https://www.aced.io/questions?role=ml-engineer): 2
- [Data Scientist](https://www.aced.io/questions?role=data-science)

Displayed company associations include [Apple](https://www.aced.io/questions?company=apple) (2), [Tesla](https://www.aced.io/questions?company=tesla), Service Now, Salesforce, Citadel, Walmart Labs, Visa, Oracle, Adobe, PayPal, JP Morgan Chase, TikTok, Capital One, Netflix, Morgan Stanley, and Dropbox.

## Interview experiences

Two shared experiences appear with this question:

- [Tesla — Software Engineer · Intern](https://www.aced.io/experiences/tesla-software-engineer-intern-interview-7815d4), **3 months ago**: the contributor described two 45-minute interviews separated by a 30-minute break. The first included 20 minutes of behavioral questions and approximately 20 minutes on an easy LeetCode question, identified as “Best time to buy and sell stocks.” The second concerned parsing a month of API data and outputting peak API hours for each day given an operation.
- [Morgan Stanley — Software Engineer · Mid Level / L4](https://www.aced.io/experiences/morgan-stanley-software-engineer-interview-8e8a40), **3 years ago**: the contributor described a process lasting one month followed by another month of background checks, properly scheduled rounds, cooperative HR, and knowledgeable, technically sound interviewers.

## Related courses

### [Machine Learning Engineer Interview Prep](https://www.aced.io/courses/ml-engineer)

![Machine Learning Engineer Interview Prep](https://images.ctfassets.net/x78yjrjc11pq/6UfkHxUyLQVfDHSfLNQtWa/1be1539a65b4f6f15201f5b573c0a578/ML_Engineer_Interview_Prep_Course.jpeg?w=800&h=800&fm=webp&q=75)

We cover mock interviews, frameworks, and advice from senior candidates for machine-learning roles at Meta, Google, Amazon, Apple, Microsoft, Nvidia, and other top companies. Topics include ML system design, core concepts, coding, and behavioral interviews.

### [Data Science Interview Prep](https://www.aced.io/courses/data-science)

![Data Science Interview Prep](https://images.ctfassets.net/x78yjrjc11pq/3U4C5C0OkDR0UeGch4AmlE/7ad489430b676a088cb973307fe77884/Data_Science_Interview_Prep_Course.jpeg?w=800&h=800&fm=webp&q=75)

We cover mock interviews, frameworks, and advice from senior candidates for data-science roles at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Topics include statistics, experimentation, coding, SQL, machine learning, and behavioral interviews.

## Aced interview-preparation resources

We provide interview questions, practice, courses, interview experiences, coaching, resume review, salary negotiation, and job referrals. Relevant destinations include [all questions](https://www.aced.io/questions), [coding questions](https://www.aced.io/questions?type=algorithms&filterBy=codeEditor&src=nav), [practice](https://www.aced.io/practice?src=nav), [courses](https://www.aced.io/courses?src=nav), [interview experiences](https://www.aced.io/experiences?src=nav), [coaching](https://www.aced.io/coaching?src=nav), and [pricing](https://www.aced.io/upgrade?src=nav).

---

[Read this page’s summary](https://www.aced.io/agents/api/public/v1/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F1731%2Fbuy-sell-stock&trace=t_679ff9ab98f3)

## Ask a follow-up about this page

Need a more specific answer? Ask the exact question you have. It returns a fresh, cited answer from Aced (formerly Exponent)’s knowledge base. No account or API key required.

Open this URL with your own question, URL-encoded. Include your question in q. All research inputs are optional; no account or setup.

`GET https://www.aced.io/agents/agent/faq?q={question}&trace=t_679ff9ab98f3`

[Tool guide and examples](https://www.aced.io/agents/agent-access) · [OpenAPI](https://www.aced.io/agents/api/public/v1/openapi.json) · [Page TLDR form](https://www.aced.io/agents/agent-tools/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F1731%2Fbuy-sell-stock). Opening a form does not execute a tool.