Python Maximum Profit Stock Interview Question
๐ Python Scenario-Based Interview Question: Find Maximum Profit from Stock Prices
If you are preparing for Python coding interviews or working on data analysis projects, this is a very common and important problem. It tests your understanding of loops, variables, and optimization.
Problem Statement
You are given stock prices with timestamps. Your task is to find the maximum profit by buying at one price and selling at a later higher price.
Input Data
prices = [
("2026-03-29 09:30:00", 150.25),
("2026-03-29 09:31:00", 152.10),
("2026-03-29 09:32:00", 151.80),
("2026-03-29 09:33:00", 154.50)
]
Expected Output
4.25
Buy at 150.25 (09:30)
Sell at 154.50 (09:33)
Python Code Solution
prices = [
("2026-03-29 09:30:00", 150.25),
("2026-03-29 09:31:00", 152.10),
("2026-03-29 09:32:00", 151.80),
("2026-03-29 09:33:00", 154.50)
]
max_profit = 0
min_price = prices[0][1]
buy_time = prices[0][0]
sell_time = prices[0][0]
for time, price in prices[1:]:
if price - min_price > max_profit:
max_profit = price - min_price
sell_time = time
if price < min_price:
min_price = price
buy_time = time
print("Maximum Profit:", round(max_profit, 2))
print("Buy Time:", buy_time)
print("Sell Time:", sell_time)
Explanation (Step-by-Step)
- We start by assuming the first price is the minimum price.
- We loop through each stock price one by one.
- For each price, we calculate the possible profit.
- If the profit is greater than the previous maximum profit, we update it.
- If the current price is lower than the minimum price, we update the minimum price.
- This ensures we always buy before selling.
Time and Space Complexity
- Time Complexity: O(n)
- Space Complexity: O(1)
- This is an optimized single-pass algorithm.
Why This Question is Important in Interviews
- Very common Python coding interview problem
- Tests logical thinking and optimization
- Used in real-world stock and financial applications
- Frequently asked in Data Analyst and Software Developer interviews
- Also known as: Best Time to Buy and Sell Stock Problem
Alternative Python Solution Using Function
def max_stock_profit(prices):
max_profit = 0
min_price = prices[0][1]
for _, price in prices[1:]:
profit = price - min_price
max_profit = max(max_profit, profit)
min_price = min(min_price, price)
return round(max_profit, 2)
print(max_stock_profit(prices))
Real-World Use Cases
- Stock market analysis
- Crypto trading bots
- Financial data analytics
- Business profit optimization
- Algorithmic trading systems
Practice Interview Question
Modify the program to:
- Return the buy and sell timestamps
- Handle negative profits
- Work with large datasets
- Read prices from a CSV file
Python Interview Question, Python Coding Problem, Stock Profit Python, Best Time to Buy and Sell Stock Python, Python Algorithm Interview, Python Data Analysis Problem, Python Programming Practice
Conclusion
This is a classic Python interview question that demonstrates how to optimize performance using a single loop. Understanding this problem will help you perform better in coding interviews and real-world data analysis projects.