• JUPYTER
  • FAQ
  • View as Code
  • Python 3 Kernel
  • View on GitHub
  • Execute on Binder
  • Download Notebook
  1. interactive-coding-challenges
  2. online_judges
  3. max_profit

This notebook was prepared by Donne Martin. Source and license info is on GitHub.

Challenge Notebook¶

Problem: Given a list of stock prices, find the max profit from 1 buy and 1 sell.¶

See the LeetCode problem page.

  • Constraints
  • Test Cases
  • Algorithm
  • Code
  • Unit Test
  • Solution Notebook

Constraints¶

  • Are all prices positive ints?
    • Yes
  • Is the output an int?
    • Yes
  • If profit is negative, do we return the smallest negative loss?
    • Yes
  • If there are less than two prices, what do we return?
    • Exception
  • Can we assume the inputs are valid?
    • No
  • Can we assume this fits memory?
    • Yes

Test Cases¶

  • None -> TypeError
  • Zero or one price -> ValueError
  • No profit
    • [8, 5, 3, 2, 1] -> -1
  • General case
    • [5, 3, 7, 4, 2, 6, 9] -> 7

Algorithm¶

Refer to the Solution Notebook. If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start.

Code¶

In [ ]:
class Solution(object):

    def find_max_profit(self, prices):
        # TODO: Implement me
        pass

Unit Test¶

The following unit test is expected to fail until you solve the challenge.

In [ ]:
# %load test_max_profit.py
import unittest


class TestMaxProfit(unittest.TestCase):

    def test_max_profit(self):
        solution = Solution()
        self.assertRaises(TypeError, solution.find_max_profit, None)
        self.assertRaises(ValueError, solution.find_max_profit, [])
        self.assertEqual(solution.find_max_profit([8, 5, 3, 2, 1]), -1)
        self.assertEqual(solution.find_max_profit([5, 3, 7, 4, 2, 6, 9]), 7)
        print('Success: test_max_profit')


def main():
    test = TestMaxProfit()
    test.test_max_profit()


if __name__ == '__main__':
    main()

Solution Notebook¶

Review the Solution Notebook for a discussion on algorithms and code solutions.

This website does not host notebooks, it only renders notebooks available on other websites.

Delivered by Fastly, Rendered by OVHcloud

nbviewer GitHub repository.

nbconvert version: 7.16.6

Rendered (Mon, 01 Dec 2025 13:55:32 UTC)