Return Buy and Sell Indices for the Best Single Stock Trade

Quick Overview

Return optimal buy and sell indices for one stock trade with a running earlier minimum, explicit no-trade behavior, and valid handling of tied profits.

Return Buy and Sell Indices for the Best Single Stock Trade

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: HR Screen

Given daily stock prices, return buy and sell indices maximizing profit for one transaction, with buy strictly before sell. If no positive-profit transaction exists, return no trade. Any maximizing index pair is acceptable. ### Constraints & Assumptions The source says a stock-selling problem must return indices but does not specify transaction count or loss policy. This practice version explicitly uses one transaction and allows no trade. Confirm these choices before applying it to another stock variant. ### Clarifying Questions to Ask - Is exactly one transaction required, or may no trade be returned? - If several index pairs tie, is any optimal pair acceptable? ```hint Keep the cheapest earlier day At each sell day, only the minimum price strictly before it is needed to evaluate that day's best trade. ``` ### What a Strong Answer Covers - Minimum price and its index, checked before incorporating the current sell day. - Best-profit pair tracking and tie handling. - Empty/singleton arrays and nonincreasing prices. ### Follow-up Questions - How would you require a trade even if every profit is negative? - What changes when multiple transactions are allowed?

Overview: Return optimal buy and sell indices for one stock trade with a running earlier minimum, explicit no-trade behavior, and valid handling of tied profits.

Google logo
Google
Nov 7, 2026
mediumSoftware EngineerHR ScreenCoding & Algorithms
1
0

Given daily stock prices, return buy and sell indices maximizing profit for one transaction, with buy strictly before sell. If no positive-profit transaction exists, return no trade. Any maximizing index pair is acceptable.

Constraints & Assumptions

The source says a stock-selling problem must return indices but does not specify transaction count or loss policy. This practice version explicitly uses one transaction and allows no trade. Confirm these choices before applying it to another stock variant.

Clarifying Questions to Ask Guidance

  • Is exactly one transaction required, or may no trade be returned?
  • If several index pairs tie, is any optimal pair acceptable?

What a Strong Answer Covers Guidance

  • Minimum price and its index, checked before incorporating the current sell day.
  • Best-profit pair tracking and tie handling.
  • Empty/singleton arrays and nonincreasing prices.

Follow-up Questions Guidance

  • How would you require a trade even if every profit is negative?
  • What changes when multiple transactions are allowed?