Count Bounded Share Transactions
Updated
Problem statement
You start with startShares shares. One transaction does exactly one of the following:
- buy one share, increasing the holding by one; or
- sell one share, decreasing the holding by one, provided the holding is positive before the sale.
Count all distinct transaction sequences whose length is between 0 and maxTransactions, inclusive, and that finish with exactly targetShares shares. The empty sequence counts when the starting and target holdings are equal.
Return the total number of valid sequences.
Function
countTransactionSequences(startShares: int, targetShares: int, maxTransactions: int) → longExamples
Example 1
startShares = 1targetShares = 2maxTransactions = 3return = 4One one-transaction sequence and three three-transaction sequences finish at two shares: B, BBS, BSB, and SBB.
Example 2
startShares = 0targetShares = 0maxTransactions = 2return = 2The empty sequence and BS are valid. A sequence cannot begin with a sale at zero shares.
Constraints
0 <= startShares <= 600 <= targetShares <= 600 <= maxTransactions <= 60- The answer fits in a signed 64-bit integer.
More Optiver Online Assessment questions
- Overheat Prevention ControllerOA · Seen Nov 2026
- Power Cell BankOA · Seen Nov 2026
- Count Straight-Line Word Occurrences in a GridOA · Seen Sep 2026
- Fastest Average Speed over a Rolling KilometerOA · Seen Sep 2026
- Multi-Level Inventory Storage SystemOA · PHONE SCREEN · Seen Aug 2026
- Proportional Momentum Investment StatisticsOA · Seen Aug 2026
- Count Ordered Size SequencesOA · Seen Aug 2026
- Profitable Currency Exchange CycleOA · Seen Aug 2026
- Construct Binary Tree S-ExpressionOA · Seen Jul 2026
- Days BetweenOA · Seen Jul 2026
Similar questions at other companies
- Frog Paths Without Long RunsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Count Routes Through Four Shop TypesOpenAI · OAOpenAI interview questions
- Count Digit Replacements with No Equal Adjacent DigitsMicrosoft · OAMicrosoft interview questions
- Count Combinations Using Weights One Through KConfluent · OAConfluent interview questions
- ABO Parent Genotype CombinationsUpstart · OAUpstart interview questions
- Count Strictly Increasing Subsequences of Length 3IBM · OAIBM interview questions
- Time Needed to Rearrange a Binary StringSalesforce · OASalesforce interview questions
- Count Shipment SchedulesMorgan Stanley · OAMorgan Stanley interview questions
- Cheating CodemakerJane Street · OAJane Street interview questions
- Count Subsequences Without Three Equal-Parity Elements in a RowGoogle · OAGoogle interview questions