Trade Compute Credits With a Mandatory Cooldown After Each Sale

Last updated: November 29, 2026

Quick Overview

You trade one unit of compute credit with known daily prices, may hold at most one unit, and after any sale you must wait one full day before buying again. Return the maximum profit over the period.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


You trade one unit of compute credit with known daily prices, may hold at most one unit, and after any sale you must wait one full day before buying again. Return the maximum profit over the period.

A Google onsite favorite for testing state-machine DP: the cooldown breaks the simple greedy that works with unlimited trades. Interviewers look for a clearly named set of states and transitions, ideally drawn before coding.

What the Interviewer Expects
  • Identify the states: holding, just sold, resting with nothing
  • Write transitions for each state per day
  • Initialize states so impossible ones cannot be chosen
  • Return the best non-holding state at the end
  • Explain why greedy summing of up-moves fails here
Key Topics to Cover
1D dynamic programming
State machine DP
Stock with cooldown
Transitions
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
  • Change the cooldown to k days.
  • Add a fixed fee on every sale.
  • Allow holding up to two units at once.
  • Recover the actual buy and sell days.
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. Each day you may buy (if holding nothing and not in cooldown), sell (if holding), or do nothing. After selling on day i you cannot buy on day i + 1. Output the max profit. Unlimi...

High-Level Design

Three states after each day: hold (own a unit), sold (sold today, so tomorrow is cooldown), rest (own nothing and free to buy tomorrow). Transitions: hold' = max(hold, rest - p) (keep, or buy ...


Submit Your Answer
Markdown supported