PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Turing

Count permutations with exactly n inversions

Last updated: Mar 29, 2026

Quick Overview

This Coding & Algorithms question evaluates combinatorial counting and algorithmic skills related to permutations and inversion counts, emphasizing dynamic programming and modular arithmetic for large-result handling.

  • medium
  • Turing
  • Coding & Algorithms
  • Software Engineer

Count permutations with exactly n inversions

Company: Turing

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given two integers **m** and **n**. Consider all permutations of the array `[1, 2, ..., m]` (each number from 1 to m appears exactly once). A permutation `perm` (0-indexed) has an **inversion** pair `(x, y)` if: - `0 <= x < y < m`, and - `perm[x] > perm[y]` A permutation is called **special** if it contains **exactly `n` inversions**. Return the **number of special permutations** modulo **10^9 + 7**. #### Examples 1) **Input:** `m = 3, n = 0` **Output:** `1` **Explanation:** Only `[1, 2, 3]` has 0 inversions. 2) **Input:** `m = 3, n = 1` **Output:** `2` **Explanation:** `[1, 3, 2]` and `[2, 1, 3]` each have exactly 1 inversion. #### Constraints - `1 <= m <= 1000` - `0 <= n <= 1000`

Quick Answer: This Coding & Algorithms question evaluates combinatorial counting and algorithmic skills related to permutations and inversion counts, emphasizing dynamic programming and modular arithmetic for large-result handling.

Turing logo
Turing
Nov 24, 2025, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
6
0

You are given two integers m and n.

Consider all permutations of the array [1, 2, ..., m] (each number from 1 to m appears exactly once). A permutation perm (0-indexed) has an inversion pair (x, y) if:

  • 0 <= x < y < m , and
  • perm[x] > perm[y]

A permutation is called special if it contains exactly n inversions.

Return the number of special permutations modulo 10^9 + 7.

Examples

  1. Input: m = 3, n = 0
    Output: 1
    Explanation: Only [1, 2, 3] has 0 inversions.
  2. Input: m = 3, n = 1
    Output: 2
    Explanation: [1, 3, 2] and [2, 1, 3] each have exactly 1 inversion.

Constraints

  • 1 <= m <= 1000
  • 0 <= n <= 1000

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Turing•More Software Engineer•Turing Software Engineer•Turing Coding & Algorithms•Software Engineer Coding & Algorithms
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.