PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Hudson

Count inversions in a permutation

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of inversion counting in permutations, testing algorithmic problem-solving, complexity analysis, and data-structure reasoning about pairwise order relations.

  • medium
  • Hudson
  • Coding & Algorithms
  • Data Scientist

Count inversions in a permutation

Company: Hudson

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem Given an array `a` of length `n` that is a permutation of distinct integers (e.g., `1..n`), define an **inversion** as a pair of indices `(i, j)` such that: - `0 ≤ i < j < n`, and - `a[i] > a[j]`. ### Task Write a function that returns the number of inversions in the array. ### Input/Output - **Input:** an integer array `a` with all distinct values - **Output:** a single integer = the number of inversions ### Notes - Any time complexity is acceptable (e.g., an `O(n^2)` approach is fine), but you may also discuss faster approaches if you know them.

Quick Answer: This question evaluates understanding of inversion counting in permutations, testing algorithmic problem-solving, complexity analysis, and data-structure reasoning about pairwise order relations.

Related Interview Questions

  • Solve Watcher Simulation And Integer Conversion - Hudson (medium)
  • Count People Reaching the Boundary - Hudson (easy)
  • Implement Buffer Parsers and Generic Map Class - Hudson (medium)
  • Implement Order Modification Feature - Hudson (hard)
  • Count players reaching end with moving watchers - Hudson (easy)
Hudson logo
Hudson
Nov 28, 2025, 12:00 AM
Data Scientist
Technical Screen
Coding & Algorithms
8
0

Problem

Given an array a of length n that is a permutation of distinct integers (e.g., 1..n), define an inversion as a pair of indices (i, j) such that:

  • 0 ≤ i < j < n , and
  • a[i] > a[j] .

Task

Write a function that returns the number of inversions in the array.

Input/Output

  • Input: an integer array a with all distinct values
  • Output: a single integer = the number of inversions

Notes

  • Any time complexity is acceptable (e.g., an O(n^2) approach is fine), but you may also discuss faster approaches if you know them.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Hudson•More Data Scientist•Hudson Data Scientist•Hudson Coding & Algorithms•Data Scientist Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ 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.