---
# System prepended metadata

title: Session 4

---

# Session 4

In a daily share trading, a buyer buys shares in the morning and sells it on the same day. If the trader is allowed to make at most 2 transactions in a day, whereas the second transaction can only start after the first one is complete (Sell->buy->sell->buy). Given stock prices throughout the day, find out the maximum profit that a share trader could have made.


Example Cases 
```
Input:   price[] = {10, 22, 5, 75, 65, 80}
Output:  87
Trader earns 87 as sum of 12 and 75
Buy at price 10, sell at 22, buy at 5 and sell at 80

Input:   price[] = {2, 30, 15, 10, 8, 25, 80}
Output:  100
Trader earns 100 as sum of 28 and 72
Buy at price 2, sell at 30, buy at 8 and sell at 80

Input:   price[] = {100, 30, 15, 10, 8, 25, 80};
Output:  72
Buy at price 8 and sell at 80.

Input:   price[] = {90, 80, 70, 60, 50}
Output:  0
Not possible to earn.
```


Make sure to practice the **[UMPIRE method](https://hackmd.io/@zXKevDKYS9a3T9goj4clQA/S1CaDzIiV?type=view)**!

:::spoiler

1. **Understand**

Ask clarifying questions and use examples to understand what the interviewer wants out of this problem
Choose a “happy path” test input, different than the one provided, and a few edge case inputs. Verify that you and the interviewer are aligned on the expected inputs and outputs.

2. **Match**

See if this problem matches a problem category (e.g. Strings/Arrays) and strategies or patterns within the category

3. **Plan**

Sketch visualizations and write pseudocode
Walk through a high level implementation with an existing diagram

4. **Implement**

Implement the solution (make sure to know what level of detail the interviewer wants)

5. **Review**

Re-check that your algorithm solves the problem by running through important examples
Go through it as if you are debugging it, assuming there is a bug

6. **Evaluate**

Finish by giving space and run-time complexity
Discuss any pros and cons of the solution
:::

**Python**
<iframe height="600px" width="100%" src="https://repl.it/@membriux/codepath-practice-session4-python?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>





