Find the Maximum Profit from Stock Transactions

Last updated: October 30, 2025

Quick Overview

Given an array of stock prices over time, find the maximum profit achievable with at most two buy-sell transactions. You must sell before buying again.

Intuit
Coding & Algorithms
Software Engineer
Intuit
October 30, 2025
Software Engineer
Online Assessment (Glider)
Coding & Algorithms
Medium

6

3

2,186 solved


Given an array of stock prices over time, find the maximum profit achievable with at most two buy-sell transactions. You must sell before buying again.

Appears on Glider assessments. Financial-themed problems are common at Intuit. Tests dynamic programming and the ability to handle constraints in financial calculations.

What the Interviewer Expects
  • Solve optimally in O(n) time and O(1) space
  • Handle edge cases like monotonically decreasing prices and single-day input
  • Explain the state transition logic clearly
  • Write clean code with descriptive variable names related to the financial domain
  • Discuss how the approach generalizes to k transactions
Key Topics to Cover
Dynamic Programming
Greedy
State Machine
Financial Algorithms
Array Processing
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
  • How would you extend this to allow at most k transactions?
  • What if there is a transaction fee for each buy-sell pair?
  • How would you handle this with a cooldown period between transactions?
  • Can you solve this using dynamic programming on states?
Sharpen Your Skills on Codemia

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

Practice DSA Problems
Sample Answer
Problem Analysis

This problem can be approached using a dynamic programming technique, specifically focusing on state transitions to track profits from stock transactions. The key is to recognize that we need to maint...

Approach
  1. Initialize Variables: Start by defining four variables to keep track of the maximum profits: first_buy, first_sell, second_buy, and second_sell. Set first_buy and second_buy to nega...

Submit Your Answer
Markdown supported

Related Questions