Question bank › Greedy
Dsa Greedy Medium

Stock Market Transactions

You are given an array of integers where each integer represents the price of a stock on the i-th day. You can complete at most one transaction (buy on one day and sell on another later day). Write a function to calculate the maximum profit you can achieve. If no profit is possible, return 0. Input: The first line contains an integer `n` (1 ≤ n ≤ 100), the number of days. The second line contains `n` integers, where each integer represents the price of the stock on that day (0 ≤ prices[i] ≤ 1000). Output: Return the maximum profit you can achieve. Example: Input: 5 7 1 5 3 6 4 Output: 5 In this example, you should buy on day 2 (price = 1) and sell on day 5 (price = 6), resulting in a profit of 6 - 1 = 5.

Key concepts

greedyoptimizationarrays

Practise this out loud — free

Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.

🎙 Practise this question now
Part of Praxari's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.