PracHub
QuestionsCoachesLearningGuidesInterview 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.

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

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding 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

  • Maintain Price Levels For A Single-Symbol Order Book - Optiver (medium)
  • Calculate Days Between Arbitrary Gregorian Dates - Optiver (medium)
  • Build and Validate a Binary Tree from Parent-Child Pairs - Optiver (medium)
  • Days Between Two Calendar Dates - Optiver (medium)
  • Thread-Safe Stock Inventory: Buy and Sell Without Overselling - Optiver (medium)