Maximize Trading Profit When Every Sale Pays a Fixed Fee

Last updated: November 29, 2026

Quick Overview

You trade one unit of an asset with known daily prices, may hold at most one unit at a time, and pay a fixed fee on each completed sale. Return the maximum profit with unlimited trades.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


You trade one unit of an asset with known daily prices, may hold at most one unit at a time, and pay a fixed fee on each completed sale. Return the maximum profit with unlimited trades.

Google interviewers use this to see whether a candidate can adapt the two-state stock DP to a cost that makes small swings unprofitable. The key signal is explaining why the fee causes merging of adjacent up-moves that greedy would trade separately.

What the Interviewer Expects
  • Define cash and hold states
  • Charge the fee on exactly one side of each trade
  • Show that greedy summing of every up-move overcounts fees
  • Use O(1) space
  • Trace on a small example
Key Topics to Cover
1D dynamic programming
State machine DP
Transaction costs
Stock buy and sell
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
  • Charge the fee as a percentage of the sale price instead.
  • Add a cooldown day after each sale as well.
  • Limit the number of trades to k.
  • Return the trade intervals.
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 and non-negative fee. Each day: buy (if holding nothing), sell (if holding, paying fee), or wait. Output the maximum final cash starting from 0 and ending with nothing held. W...

High-Level Design

Keep cash (best profit while holding nothing after today) and hold (best profit while holding one unit). Transitions: cash' = max(cash, hold + p - fee) and hold' = max(hold, cash - p). Initial...


Submit Your Answer
Markdown supported