
Asked in Quikr
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.
Be the first one to answer
Add answer anonymously...
Top Software Developer Interview Questions Asked at Quikr
Q. Given a number K and an array, find a pair of elements whose sum is equal to K.
Q. If a website is run by two servers that balance the load using a Load Balancer, ...read more
Q. Write a program to implement basic Design Patterns like Singleton.
Interview Questions Asked to Software Developer at Other Companies
Top Skill-Based Questions for Quikr Software Developer
Algorithms Interview Questions and Answers
250 Questions
Data Structures Interview Questions and Answers
250 Questions
Web Development Interview Questions and Answers
250 Questions
Java Interview Questions and Answers
250 Questions
SQL Interview Questions and Answers
250 Questions
Software Development Interview Questions and Answers
250 Questions
Stay ahead in your career. Get AmbitionBox app


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
AmbitionBox Awards
Get AmbitionBox app

