Count BUY and SELL trade sequences that never go net-short

Quick Overview

Count the orderings of n BUY and SELL trades with exactly k sells in which buys never fall behind sells at any prefix, returned modulo 10^9 + 7. It tests dynamic programming over lattice paths and ballot-style counting arguments.

Count BUY and SELL trade sequences that never go net-short

Company: Optiver

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A trader executes exactly `n` share trades in a single day. Each trade is either a BUY or a SELL. To remain compliant with regulations, the running count of BUY trades must always be greater than or equal to the running count of SELL trades at every point in the sequence (so the trader can never be net-short). At the end of the day, the trader must have executed exactly `k` SELL trades (and therefore `n - k` BUY trades). Given integers `n` and `k`, return the number of distinct valid sequences of trades, modulo `10^9 + 7`. ### Function Signature ```python def distinct_trade_sequences(n: int, k: int) -> int: ``` ### Rules - A sequence is a string of `n` trades, each `B` (BUY) or `S` (SELL). Two sequences are different if they differ at any position. - The condition applies to every prefix, including the whole sequence. Equal counts are allowed. - If no valid sequence exists, return `0`. ### Constraints - `1 <= n <= 1000` - `1 <= k <= n` ### Examples ```text Input: n = 4, k = 2 Output: 2 ``` With 2 BUYs and 2 SELLs, the sequences that keep BUYs >= SELLs at every prefix are BBSS and BSBS. The other four orderings (BSSB, SBBS, SBSB, SSBB) each have a prefix with more SELLs than BUYs. ```text Input: n = 6, k = 3 Output: 5 ``` With 3 BUYs and 3 SELLs, the valid sequences are BBBSSS, BBSBSS, BBSSBS, BSBBSS and BSBSBS. ```text Input: n = 5, k = 2 Output: 5 ``` With 3 BUYs and 2 SELLs, the valid sequences are BBBSS, BBSBS, BBSSB, BSBBS and BSBSB. The other five orderings either start with a SELL or begin with BSS.

Overview: Count the orderings of n BUY and SELL trades with exactly k sells in which buys never fall behind sells at any prefix, returned modulo 10^9 + 7. It tests dynamic programming over lattice paths and ballot-style counting arguments.

Optiver logo
Optiver
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

A trader executes exactly n share trades in a single day. Each trade is either a BUY or a SELL. To remain compliant with regulations, the running count of BUY trades must always be greater than or equal to the running count of SELL trades at every point in the sequence (so the trader can never be net-short).

At the end of the day, the trader must have executed exactly k SELL trades (and therefore n - k BUY trades). Given integers n and k, return the number of distinct valid sequences of trades, modulo 10^9 + 7.

Function Signature

def distinct_trade_sequences(n: int, k: int) -> int:

Rules

  • A sequence is a string of n trades, each B (BUY) or S (SELL). Two sequences are different if they differ at any position.
  • The condition applies to every prefix, including the whole sequence. Equal counts are allowed.
  • If no valid sequence exists, return 0 .

Constraints

  • 1 <= n <= 1000
  • 1 <= k <= n

Examples

Input: n = 4, k = 2
Output: 2

With 2 BUYs and 2 SELLs, the sequences that keep BUYs >= SELLs at every prefix are BBSS and BSBS. The other four orderings (BSSB, SBBS, SBSB, SSBB) each have a prefix with more SELLs than BUYs.

Input: n = 6, k = 3
Output: 5

With 3 BUYs and 3 SELLs, the valid sequences are BBBSSS, BBSBSS, BBSSBS, BSBBSS and BSBSBS.

Input: n = 5, k = 2
Output: 5

With 3 BUYs and 2 SELLs, the valid sequences are BBBSS, BBSBS, BBSSB, BSBBS and BSBSB. The other five orderings either start with a SELL or begin with BSS.