Count nonnegative buy/sell sequences
Company: Optiver
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates combinatorial reasoning and algorithmic analysis by asking for the count of constrained buy/sell sequences, testing competence in counting under constraints and reasoning about sequence invariants.
Constraints
- 0 <= n <= 1000
- You must return the exact count, not modulo any value
- The empty sequence for n = 0 counts as one valid sequence
Examples
Input: (0,)
Expected Output: 1
Explanation: With 0 trades, the only valid sequence is the empty sequence.
Input: (1,)
Expected Output: 1
Explanation: The only valid sequence is buy, then sell.
Hints
- Try viewing a buy as '(' and a sell as ')'. What well-known counting problem does that become?
- Instead of generating sequences, use the recurrence for Catalan numbers to compute the answer in O(n) time.