Sure Profit from a Stock and a Casino Bet
Updated
Problem statement
A stock costs today dollars now. Tomorrow its price will be one of the values in tomorrow, and you do not know which. No value in tomorrow equals today, so the price always goes either up or down.
Today you may do both of these:
- Buy or short-sell up to one share at today's price. Any amount from
-1to1share is allowed (negative means short). Holdingqshares earnsq × (tomorrow's price - today). - Place one bet of any size at a casino on whether the price goes up or down. The bet pays even money: if you bet
xdollars and are right, you winx; if you are wrong, you losex.
Your profit is the share result plus the bet result. Return the largest profit you can guarantee, whichever price in tomorrow happens. If no plan guarantees a positive profit, return 0.
Your answer is accepted if its absolute error or its relative error is at most 10^-9.
Function
sureProfit(today: int, tomorrow: int[]) → doubleExamples
Example 1
today = 100tomorrow = [50,200]return = 25.0This is the interview case. Buy one share and bet $75 that the stock goes down.
- Price 200: the share earns
100, the bet loses75, total25. - Price 50: the share loses
50, the bet wins75, total25.
No plan does better. With one share and a bet of b on down, the two results are 100 - b and -50 + b, and the smaller of two numbers is at most their average, 25. Fewer shares scale this down, and short selling makes the average negative. So the answer is 25.
Example 2
today = 100tomorrow = [90,105]return = 2.5The middle of 90 and 105 is 97.5, below today's price of 100, so this time the stock is the expensive side. Short one share and bet $7.50 that the stock goes up.
- Price 105: the short loses
5, the bet wins7.5, total2.5. - Price 90: the short earns
10, the bet loses7.5, total2.5.
Example 3
today = 100tomorrow = [60,90,150,170]return = 5.0Buy one share and bet $45 on down. The worst up price is 150: 50 - 45 = 5. The worst down price is 60: -40 + 45 = 5. The other prices do better (170 gives 25 and 90 gives 35), so the guaranteed profit is 5. Shorting one share would guarantee at most 100 - (170 + 90) / 2 = -30, and a smaller short only scales that loss down.
Constraints
2 <= today <= 99992 <= tomorrow.length <= 1001 <= tomorrow[i] <= 10^4tomorrow[i] != today, and at least one price intomorrowis abovetodayand at least one is below it
More Susquehanna International Group (SIG) Phone Screen questions
- Dice Sum That Is a Multiple of mPHONE SCREEN · Seen Nov 2026
- Find Median from Data StreamPHONE SCREEN · Seen Nov 2026
- Painting Arcs to Nearest Neighbors on a CirclePHONE SCREEN · Seen Sep 2026
- Filter Movies by Inclusive Year RangePHONE SCREEN · Seen Jul 2026
- Best Single Reroll for Five DicePHONE SCREEN · Seen Feb 2026
- Catching the Bus to the MoviePHONE SCREEN · Seen Jan 2026
- Two Dice Summing to a TargetPHONE SCREEN · Seen Jan 2026
- Median of Uniform Draws in an IntervalPHONE SCREEN · Seen Oct 2025
- One Number Is the Average of the Other TwoPHONE SCREEN · Seen Jun 2025
- Which Coin Pattern Shows Up FirstPHONE SCREEN · Seen May 2025
Similar questions at other companies
- Logistic Regression with Gradient DescentYahoo · PHONE SCREENYahoo interview questions
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Adaptive Calibration ErrorOpenAI · PHONE SCREENOpenAI interview questions
- Random Pick with Weight Using Recorded DrawsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Reachable Transfer PositionsCitadel · PHONE SCREENCitadel interview questions
- Find All Pythagorean TripletsDell Technologies · PHONE SCREENDell Technologies interview questions
- Sort a Quadratic-Transformed ArrayLinkedIn · PHONE SCREENLinkedIn interview questions
- Filter Paginated Medical RecordsNVIDIA · PHONE SCREENNVIDIA interview questions
- Minimum Operations to Make Every Array Element OneOtter.ai · PHONE SCREENOtter.ai interview questions
- Evenly Schedule Vehicle InspectionsAmazon · PHONE SCREEN