Best Time to Buy and Sell Stock Problem Statement

Given an array prices representing the prices of a stock where each element indicates the price at a given minute, determine the maximum profit you can achieve by buying and selling the stock once.

Example:

Input:
prices = [2, 100, 150, 120]
Output:
148
Explanation:

The maximum profit can be achieved by buying at minute 0 for Rs. 2 and selling at minute 2 for Rs. 150, resulting in a profit of 150 - 2 = 148.

Constraints:

  • 1 <= T <= 10
  • 2 <= N <= 10^4
  • 1 <= prices[i] <= 10^9

Note: You cannot sell before buying.

If the maximum profit is negative, consider it as 0.

AnswerBot
4mo

Find the maximum profit by buying and selling a stock once based on given prices.

  • Iterate through the prices array and keep track of the minimum price seen so far and the maximum profit achievable.

  • Calc...read more

chamktasuraj
9mo
works at
Why not sort and return the difference between first and last.
Help your peers!
Select
Add answer anonymously...

Tower Research Capital LLC Software Engineer interview questions & answers

A Software Engineer was asked Q. Given k and a DFS traversal string for a k-ary tree, construct the tree. The str...read more
A Software Engineer was asked Q. How would you determine if a graph contains a node with degree n?
A Software Engineer was asked Q. Can you describe the classes and functions involved in the design of a system li...read more

Popular interview questions of Software Engineer

A Software Engineer was asked Q1. Given k and a DFS traversal string for a k-ary tree, construct the tree. The str...read more
A Software Engineer was asked Q2. How would you determine if a graph contains a node with degree n?
A Software Engineer was asked Q3. Can you describe the classes and functions involved in the design of a system li...read more
Tower Research Capital LLC 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