{"record":{"id":"8d92574c3469930d","repo":"donnemartin/interactive-coding-challenges","slug":"prices-must-have-at-least-two-values","errorCode":null,"errorMessage":"prices must have at least two values","messagePattern":"prices must have at least two values","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"online_judges/max_profit/max_profit_solution.ipynb","lineNumber":105,"sourceCode":"    \"## Code\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 1,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"import sys\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"class Solution(object):\\n\",\n    \"\\n\",\n    \"    def find_max_profit(self, prices):\\n\",\n    \"        if prices is None:\\n\",\n    \"            raise TypeError('prices cannot be None')\\n\",\n    \"        if len(prices) < 2:\\n\",\n    \"            raise ValueError('prices must have at least two values')\\n\",\n    \"        min_price = prices.pop(0)\\n\",\n    \"        max_profit = prices[0] - min_price\\n\",\n    \"        for price in prices:\\n\",\n    \"            profit = price - min_price\\n\",\n    \"            min_price = min(price, min_price)\\n\",\n    \"            max_profit = max(profit, max_profit)\\n\",\n    \"        return max_profit\"\n   ]\n  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Unit Test\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/online_judges/max_profit/max_profit_solution.ipynb#L87-L123","documentation":"Raised by Solution.find_max_profit when the prices list has fewer than 2 entries; a single price gives no buy/sell pair, so max profit is undefined. It is a defensive ValueError guard at the top of the method. The algorithm then pops the first element as the initial min price and iterates the rest.","triggerScenarios":"Calling find_max_profit with a list of length 0 or 1, e.g. find_max_profit([5]) or find_max_profit([]).","commonSituations":"Passing an empty dataset from a file/API feed, or a test case that assumed a single price should return 0. Also note the method mutates its input via prices.pop(0).","solutions":["Ensure the caller passes at least two prices before invoking find_max_profit.","If a 0 profit is the desired behavior for <2 prices, catch the ValueError or check len(prices) >= 2 first and short-circuit to 0.","Pass a copy (list(prices)) since the method pops from the input list."],"exampleFix":"# before\nprofit = Solution().find_max_profit([7])  # ValueError\n\n# after\nprices = [7]\nprofit = Solution().find_max_profit(prices) if len(prices) >= 2 else 0","handlingStrategy":"validation","validationCode":"if not isinstance(prices, (list, tuple)) or len(prices) < 2:\n    raise ValueError('need at least two prices')\nprofit = Solution().find_max_profit(list(prices))","typeGuard":"def has_two_prices(p):\n    return isinstance(p, list) and len(p) >= 2","tryCatchPattern":"try:\n    profit = s.find_max_profit(prices)\nexcept ValueError as e:\n    profit = 0  # or log and skip","preventionTips":["Validate list length before calling domain functions.","Pass list(prices) to avoid the method's pop(0) mutating your input."],"tags":["python","input-validation","algorithm","valueerror"],"backgroundTag":"invalid-input-length","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}