Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7440675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:53:16+00:00 2026-05-29T10:53:16+00:00

I was asked this problem in a interview: Best Time to Buy and Sell

  • 0

I was asked this problem in a interview:

Best Time to Buy and Sell Stock

Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to buy one share of the stock and sell one share of the stock, design an algorithm to find the best times to buy and sell. I was able to give an O(n) algorithm.

While the interviewer asked me a followed on question, what is the case with “buy one, sell one”, and then “buy one, sell one”, which means there are two trades in one day, maximize the profit. I was able to give an O(n^2) algorithm. But the interviewer said it can be improved. Is there a O(n) algorithm?

The interviewer said that you can not buy two share at the same time. You must buy one sell it, and then buy one at another time, then sell it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:53:17+00:00Added an answer on May 29, 2026 at 10:53 am

    The O(n) solution given in the original question’s reference gives the optimal “buy one then sell one” answer for each prefix of the original array. That algorithm could also be trivially modified to cope with the “backwards” situation; i.e. a “sell one then buy one” where you are working backwards from the array; that’s equivalent to having the “buy one then sell one” answer for each suffix of the array.

    Now, in the “buy, sell, buy, sell” case we have some point (after the first sell) where we are somewhere within our array, say b. For that breakpoint the best solution would be the best prefix solution for 0..b and the best suffix solution for b+1..n. The best “buy, sell, buy, sell” overall is the optimum of these optimum solutions.

    So, to solve the “buy, sell, buy, sell” in O(n), you can solve the prefix in O(n), suffix in O(n), and then for each breakpoint calculate the optimum – so nO(1). That’s an O(n) algorithm using O(n) space.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem asked to me in an interview, this is a similar
I have asked this question before and the problem was half solved in the
I've been into this problem for a while already, and have asked some questions
This is a question that I was asked on a job interview some time
This is a interview Question which was asked and wanted to find an efficient
I can't figure this interview question. You have an array of integers. You need
This is an interview problem I was asked, and I am still trying to
I have asked this before however I am still having issues with this problem!
I was asked this question in an interview today. I have tried a solution
This problem is same as asked in here . Given a list of coins,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.