FastPrepBest Stock Trade with Buy and Sell Days
Problems/Best Stock Trade with Buy and Sell Days

Best Stock Trade with Buy and Sell Days

Google logoGoogle● EasyFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionExamples

Problem statement

Source note: Google asked this as the first of two coding questions in an L4 screening interview. It is the classic best time to buy and sell a stock, except that the answer gives the buy and sell positions, not just the profit.

The report gave no limits or tie rules. We chose the usual single-trade limits: up to 10^5 days and prices from 0 to 10^4. Days are counted from 0. Ties go to the earliest buy day, then the earliest sell day. If no trade makes money, the answer is [-1, -1].

Our version is about 85% the same as the reported question.

You are given an array prices where prices[i] is the price of a stock on day i. You may buy the stock on one day and sell it on a later day, at most once.

The problem statement continues
Pro

Examples

Example 1

prices = [7,1,5,3,6,4]return = [1,4]
  • Buy on day 1 at price 1 and sell on day 4 at price 6 for a profit of 5, the largest possible.
02 · Example 2
FastPrep Pro
Reported in 1 Google interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 2 more worked examples, explained
  • Guided hints and editorial
  • Run your code on real test cases
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week

More Google Phone Screen questions

  • Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Logger Rate Limiter with Every-Arrival HistoryPHONE SCREEN · Seen Nov 2026
  • Minimum Total Layover WaitPHONE SCREEN · Seen Nov 2026
  • Reach a Destination Through Scheduled FlightsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Minimum Boards to Cover Roof HolesPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Can Place FlowersAgoda · PHONE SCREENAgoda interview questions
  • Maximum Later Checkpoint Altitude GainYellow.ai · PHONE SCREEN
  • Maximum Profit with Exactly One Stock TransactionRetool · PHONE SCREEN · ONSITE INTERVIEWRetool interview questions
  • One Package for All ProductsAmazon · PHONE SCREEN
  • Best Time to Buy and Sell StockOracle · PHONE SCREEN · ONSITE INTERVIEWOracle 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
  • Choose the Front Football TeamAnduril · PHONE SCREENAnduril interview questions
  • Best Time to Buy and Sell StockBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Best Time to Buy and Sell StockHeadlands Technologies · PHONE SCREEN · ONSITE INTERVIEWHeadlands Technologies interview questions

Interview reports that mention this question

  • Google L4 SWE Phone Screen: 1 Experience

Google interview experiences

  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
  • Google AI Coding Interview and AI OA: 2 Experiences
  • Google SWE Intern Interview Results Timeline: 13 Experiences
  • Google L4 SWE Phone Screen and Kirkland Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
CodePython 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import heapq
from collections import defaultdict
 
class Solution:
def solve(self, values, queries):
# Build the adjacency structure
n = len(values)
graph = defaultdict(list)
for u, v in queries:
graph[u].append(v)
 
best = [float("inf")] * n
heap = [(0, 0)]
while heap:
cost, node = heapq.heappop(heap)
if cost > best[node]:
continue
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 Google interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 2 more worked examples, explained
  • Guided hints and editorial
  • Run your code on real test cases
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week