Count orderings of n pickups and deliveries where each delivery follows its pickup

Quick Overview

Count the orderings of n pickup and delivery events in which every delivery comes after its own pickup, returning the result modulo a large prime. Tests combinatorial counting, turning a counting argument into a linear-time computation, and overflow-safe modular arithmetic.

Count orderings of n pickups and deliveries where each delivery follows its pickup

Company: DoorDash

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

There are `n` orders. Each order `i` has two events: a pickup `P_i` and a delivery `D_i`. A sequence is an ordering of all `2n` events, with each event appearing exactly once. A sequence is valid when, for every order `i`, the delivery `D_i` comes after the pickup `P_i`. Count the valid sequences. The answer can be large, so return it modulo `10^9 + 7`. ### Function Signature ```python def count_orders(n: int) -> int: ``` ### Rules - Two sequences are different when some position holds a different event. Orders are distinguishable, so `P1 P2 D1 D2` and `P2 P1 D1 D2` count separately. - Return the count reduced modulo `10^9 + 7`. ### Constraints - `1 <= n <= 500` ### Examples ```text Input: n = 2 Output: 6 ``` The six valid sequences are `P1 P2 D1 D2`, `P1 P2 D2 D1`, `P1 D1 P2 D2`, `P2 P1 D1 D2`, `P2 P1 D2 D1` and `P2 D2 P1 D1`. The other 18 orderings of the four events put at least one delivery before its pickup. ```text Input: n = 3 Output: 90 ``` Of the 720 orderings of the six events, 90 have every delivery after its own pickup. ```text Input: n = 8 Output: 729647433 ``` Before the modulus the count is `81729648000`, and `81729648000 mod (10^9 + 7) = 729647433`.

Overview: Count the orderings of n pickup and delivery events in which every delivery comes after its own pickup, returning the result modulo a large prime. Tests combinatorial counting, turning a counting argument into a linear-time computation, and overflow-safe modular arithmetic.

DoorDash logo
DoorDash
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

There are n orders. Each order i has two events: a pickup P_i and a delivery D_i. A sequence is an ordering of all 2n events, with each event appearing exactly once.

A sequence is valid when, for every order i, the delivery D_i comes after the pickup P_i. Count the valid sequences. The answer can be large, so return it modulo 10^9 + 7.

Function Signature

def count_orders(n: int) -> int:

Rules

  • Two sequences are different when some position holds a different event. Orders are distinguishable, so P1 P2 D1 D2 and P2 P1 D1 D2 count separately.
  • Return the count reduced modulo 10^9 + 7 .

Constraints

  • 1 <= n <= 500

Examples

Input: n = 2
Output: 6

The six valid sequences are P1 P2 D1 D2, P1 P2 D2 D1, P1 D1 P2 D2, P2 P1 D1 D2, P2 P1 D2 D1 and P2 D2 P1 D1. The other 18 orderings of the four events put at least one delivery before its pickup.

Input: n = 3
Output: 90

Of the 720 orderings of the six events, 90 have every delivery after its own pickup.

Input: n = 8
Output: 729647433

Before the modulus the count is 81729648000, and 81729648000 mod (10^9 + 7) = 729647433.