PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • Medium
  • Optiver
  • Coding & Algorithms
  • Software Engineer

Count nonnegative buy/sell sequences

Company: Optiver

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

You must perform exactly 2n unit trades starting with 0 shares and ending with 0 shares. Each trade is either a buy (+1 share) or a sell (−1 share), and your running position may never be negative at any point. Given n, how many distinct buy/sell sequences satisfy these constraints? Return the count for the given n and describe an efficient approach.

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.

You must perform exactly 2n unit trades. You start with 0 shares, each buy increases your position by 1, and each sell decreases it by 1. After all 2n trades, you must end back at 0 shares, and your running position may never become negative at any point. Return the number of distinct buy/sell sequences that satisfy these rules for a given n. An efficient solution should avoid generating all possible sequences, since there are exponentially many of them. This counting problem is equivalent to counting valid parentheses strings with n pairs.

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.

Input: (2,)

Expected Output: 2

Explanation: The valid sequences are buy-buy-sell-sell and buy-sell-buy-sell.

Input: (3,)

Expected Output: 5

Explanation: There are 5 valid sequences, which is the 3rd Catalan number.

Input: (4,)

Expected Output: 14

Explanation: There are 14 valid sequences for 4 buys and 4 sells.

Input: (8,)

Expected Output: 1430

Explanation: This is the 8th Catalan number.

Solution

def solution(n):
    if n < 0:
        raise ValueError("n must be nonnegative")

    catalan = 1
    for k in range(n):
        catalan = catalan * 2 * (2 * k + 1) // (k + 2)
    return catalan

Time complexity: O(n). Space complexity: O(1).

Hints

  1. Try viewing a buy as '(' and a sell as ')'. What well-known counting problem does that become?
  2. Instead of generating sequences, use the recurrence for Catalan numbers to compute the answer in O(n) time.
Last updated: Jun 6, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find missing numbers in sequences - Optiver (hard)
  • Design a circular queue data structure - Optiver (medium)
  • Optimize flight and cargo bookings for profit - Optiver (hard)
  • Compare two programs for equivalence - Optiver (Medium)
  • Design a satellite propagation simulator - Optiver (Medium)