Buy and Sell Stock Problem Statement

Imagine you are Harshad Mehta's friend, and you have been given the stock prices of a particular company for the next 'N' days. You can perform up to two buy-and-sell transactions to maximize your profit. Your task is to determine the maximum profit that can be achieved. Remember, you must sell the stock before you can buy again.

You can enlist Mr. Mehta's help if needed.

Input:

T (number of test cases)
For each test case:
N (number of days)
N space-separated integers signifying stock prices for each day

Output:

For each test case, output the maximum profit possible on a new line

Example:

Input:
T = 2
Test case 1: N = 5, Prices = [3, 3, 5, 0, 0]
Test case 2: N = 5, Prices = [7, 6, 4, 3, 1]
Output:
Max profit for test case 1: 6
Max profit for test case 2: 0

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 5 * 104
  • 0 ≤ price ≤ 103

Note:

- Buying a stock and then selling it constitutes one transaction.
- You cannot engage in multiple simultaneous transactions, meaning you need to sell a stock before you buy again.
- Ensure the function is implemented as specified, there is no need for additional print operations.
AnswerBot
4mo

The task is to determine the maximum profit that can be achieved by performing up to two buy-and-sell transactions on a given set of stock prices.

  • Iterate through the array of stock prices to find the ...read more

specificroller
6mo
works at
Hello kskdd
Help your peers!
Select
Add answer anonymously...

Info Edge Senior Software Engineer interview questions & answers

A Senior Software Engineer was asked 2mo agoQ. Given two strings s and t, return the minimum window in s which will contain all...read more
A Senior Software Engineer was asked 12mo agoQ. Describe how you would build an MVC architecture.
A Senior Software Engineer was asked Q. Given a sorted array of integers nums and an integer target, write a function to...read more

Popular interview questions of Senior Software Engineer

A Senior Software Engineer was asked 2mo agoQ1. Given two strings s and t, return the minimum window in s which will contain all...read more
A Senior Software Engineer was asked 12mo agoQ2. Describe how you would build an MVC architecture.
A Senior Software Engineer was asked Q3. Given a sorted array of integers nums and an integer target, write a function to...read more
Info Edge Senior Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits