FastPrepMaximum Profit with Exactly One Stock Transaction
Problems/Maximum Profit with Exactly One Stock Transaction

Maximum Profit with Exactly One Stock Transaction

Retool logoRetool● EasyFULLTIMENEW GRADPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Several interview reports require exactly one stock transaction, even when it loses money. A later report also asks you to show the chosen trade. Here you return the maximum profit with its buy and sell day indices. Days start at zero, and ties go to the earliest buy and the earliest sale. Those are our choices. Our version is about 90% the same as the combined reports.

Given daily stock prices, choose exactly one buy day and one later sell day to maximize sellPrice - buyPrice. A transaction is required even when every choice loses money, so its profit may be negative.

Return [profit, buyDay, sellDay], where days are zero-based indices and buyDay < sellDay. The first component is the maximum profit; the other two identify the chosen transaction. If several transactions have that profit, choose the smallest buy day, then the smallest sell day.

The selected-day output makes the chosen trade explicit rather than returning only its profit.

Function

maxProfitExactlyOne(prices: int[]) → int[]

Examples

Example 1

prices = [7,1,5,3,6,4]return = [5,1,4]

The best required transaction buys on day 1 and sells on day 4, for profit 5. The selected days use the stated earliest-day tie rule.

Example 2

prices = [7,6,4,3,1]return = [-1,0,1]

The best required transaction buys on day 0 and sells on day 1, for profit -1. The selected days use the stated earliest-day tie rule.

Example 3

prices = [3,3]return = [0,0,1]

The best required transaction buys on day 0 and sells on day 1, for profit 0. The selected days use the stated earliest-day tie rule.

Example 4

prices = [1,4,1,4]return = [3,0,1]

Days [0,1], [0,3] and [2,3] all earn three. Choose the earliest buy, then earliest sale.

Constraints

  • 2 <= prices.length <= 100000.
  • 0 <= prices[i] <= 1000000.
  • The answer fits in a signed 32-bit integer.

More Retool interview questions

  • Order Sources by Their DependenciesPHONE SCREEN · Seen Oct 2026
  • Execute In-Memory SQL QueriesPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2026
  • Maximum Profit with Exactly Two Stock TransactionsPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2026
  • Next Consistent Wordle GuessPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2024
  • Wordle FeedbackPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2024
  • Wordle Guess LoopPHONE SCREEN · Seen Jun 2024
  • Markov Transition Counts and ProbabilitiesONSITE INTERVIEW · Seen Mar 2026

Similar questions at other companies

  • Best Stock Trade with Buy and Sell DaysGoogle · PHONE SCREENGoogle interview questions
  • Can Place FlowersAgoda · PHONE SCREENAgoda interview questions
  • Maximum Later Checkpoint Altitude GainYellow.ai · PHONE SCREEN
  • Assign CookiesAmazon · PHONE SCREEN
  • Best Time to Buy and Sell Stock IIGroww · ONSITE INTERVIEW
  • Best Time to Buy and Sell StockOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions
  • Cheapest Round TripUber · ONSITE INTERVIEWUber interview questions
  • Best Time to Buy and Sell Stock IIPoint72 · PHONE SCREENPoint72 interview questions
  • Best Time to Buy and Sell StockJP Morgan Chase · PHONE SCREENJP Morgan Chase interview questions
  • Best Time to Buy and Sell StockZoox · ONSITE INTERVIEW
All Retool interview questionsSee Retool hiring insights
Saved
class Solution {
    public int[] maxProfitExactlyOne(int[] prices) {
        // Write your code here.
    }
}
Checking account…