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

  • Home
  • SEARCH
  • 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 6581405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:08:55+00:00 2026-05-25T16:08:55+00:00

I got a question on an algorithm question I got in an interview and

  • 0

I got a question on an algorithm question I got in an interview and I can’t seem to figure it out. I understand how it should work, but can’t get it sorted algorithmically.

So assume a firm trades oil barrels and is only able to retain one oil barrel at a time. Assume the company knows the price per barrel for each and every day in a year. So its passed into as an array. How can one write an algorithm to find when to buy and sell?

Here’s an example for just 5 days for simplification:
70 74 73 72 76, for days Monday through Friday respectively.

The best thing to do here is to to buy on Monday (70) sell on Tuesday (74) and then buy on Thursday (72) and sell on Friday (76). Should that be approached recursively? I really want to solve this.

Thanks,

  • 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-25T16:08:56+00:00Added an answer on May 25, 2026 at 4:08 pm

    I assume you want to maximise your profit, right?

    In that case, you just buy at local minima and sell at local maxima, which would be a simple linear search.

    Actually, it is that simple. Proof:

    Let’s denote

    p(i) ... the price of oil on day i
    have(i) ... 1 if we retain the barrel overnight from day i to day i+1, 0 otherwise
    

    have is only defined for i in [0, N-1]

    now, if we buy on day k and sell on day l, we’d have

    have(k) = 1
    have(l) = 0
    have(i) = 1 for k < i < l
    

    the profit would be

    p(l)-p(k) = sum over {i from k to l-1} (p(i+1)-p(i))
    

    Let’s denote

    M(i) = max(p(i+1) - p(i), 0)
    

    For all possible boolean functions have, we have

    profit(have) = sum over {i where have(i)==1} (p(i+1) - p(i))
     <= sum over {i where have(i)==1} max(p(i+1) - p(i), 0)
     <= sum over {i where have(i)==1} M(i)
     <= sum over {i in [0, N-1]} M(i)
    

    The second line comes from the fact that max(x, 0) >= x, the third is simple rewrite in terms of M(i), the fourth comes from M(i) >= 0.

    Now, if we set have(i) == (p(i+1)>p(i)), it would have the same profit as above, which means it is maximal. Also, that means you buy at local minima and sell at local maxima.

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

Sidebar

Related Questions

I have probem / strange question, i got algorithm with few for loops and
i got a question about unsigned char array. How can i store an integer
Possible Duplicate: Easy interview question got harder: given numbers 1..100, find the missing number(s)
My girlfriend got this question in an interview, and I liked it so much
I got this question in an interview with amazon. I was asked to perform
I got this question in an interview and I was not able to solve
I've got a question about coding an algorithm for a Texas Instruments TMS320C64xx DSP
I've got an algorithm that compares two sorted lists , and I'm planning to
I'm writing in perl, but it seems more like an algorithm question to me.
I have to figure out an algorithm, which finds a solution for the Connect

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.