The Trade Signal
A easy Python interview practice problem on DataDriven. Write and execute real python code with instant grading.
- Domain
- Python
- Difficulty
- easy
- Seniority
- L4
Problem
Given a list of prices indexed by day, find the maximum gain from a single buy-then-sell window where you must buy before you sell. Return a 3-element list [gain, buy_day_index, sell_day_index], where buy_day_index and sell_day_index are the positions in the input list. If no profitable trade is possible (gain <= 0, including a flat or strictly declining series), return [0, -1, -1] to signal no trade. Among multiple windows tied for max gain, return the one with the earliest buy index, then earliest sell index.
Summary
Buy low, sell high. Identify the ideal moment.
Practice This Problem
Solve this Python problem with real code execution. DataDriven runs your Python code in a real environment and grades it automatically.