Maximize Profit with One Stock Transaction

Quick Overview

Implement `max_profit(prices)` for daily integer prices. Work through the function contract, boundary cases, correctness argument, and time and space complexity expected in a production-quality solution.

Maximize Profit with One Stock Transaction

Company: Oracle

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

# Maximize Profit with One Stock Transaction Implement `max_profit(prices)` for daily integer prices. Choose at most one buy followed by one later sell and return the maximum nonnegative profit. Return `0` when no profitable trade exists. Constraints: up to `200000` prices; each price is an integer in `[0, 10^12]`. The returned profit is a signed 64-bit integer and is guaranteed not to exceed `2^53 - 1`, so all four language implementations represent it exactly. Aim for `O(n)` time and `O(1)` space. ```hint Respect transaction order Test cases include a decreasing series and a low price that appears only after the best selling opportunity. ```

Quick Answer: Implement `max_profit(prices)` for daily integer prices. Work through the function contract, boundary cases, correctness argument, and time and space complexity expected in a production-quality solution.

|Home/Coding & Algorithms/Oracle
Oracle logo
Oracle
Jul 26, 2026, 12:00 AM
mediumSoftware EngineerOnsiteCoding & Algorithms
0
0

Maximize Profit with One Stock Transaction

Implement max_profit(prices) for daily integer prices. Choose at most one buy followed by one later sell and return the maximum nonnegative profit. Return 0 when no profitable trade exists.

Constraints: up to 200000 prices; each price is an integer in [0, 10^12]. The returned profit is a signed 64-bit integer and is guaranteed not to exceed 2^53 - 1, so all four language implementations represent it exactly. Aim for O(n) time and O(1) space.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...