Single Best Buy and Sell of Preemptible VM Credits

Last updated: November 29, 2026

Quick Overview

Given the daily price of preemptible VM credits on Google Cloud over n days, you may buy once and later sell once. Return the maximum profit, or 0 if no profitable trade exists.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


Given the daily price of preemptible VM credits on Google Cloud over n days, you may buy once and later sell once. Return the maximum profit, or 0 if no profitable trade exists.

A standard online assessment item framed around cloud pricing. Graders check for the single pass that tracks the cheapest price so far, and that you avoid the O(n^2) pair scan.

What the Interviewer Expects
  • Recognize the sell day must come after the buy day
  • Track the minimum price seen so far as running state
  • Compute best profit in one pass
  • Return 0 when prices only fall
Key Topics to Cover
1D dynamic programming
Running minimum
Stock buy and sell
Single pass
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if you may make unlimited trades, but never hold more than one unit?
  • Return the buy and sell days, not only the profit.
  • What if there is a transaction fee per sale?
  • Prices arrive as a stream. What do you keep in memory?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: list prices of non-negative integers, one per day. Pick i < j to maximize prices[j] - prices[i]; if every choice loses money, return 0. Length may be 0 or 1, in which case no trade is possi...

High-Level Design

Think of it as DP over days: best_until[j] = max(best_until[j-1], prices[j] - min_before[j]), where min_before[j] is the lowest price on days before j. Both values are one-step updates, so a singl...


Submit Your Answer
Markdown supported