FastPrepCount Array Inversions
Problems/Count Array Inversions

Count Array Inversions

Tata Consultancy Services logoTata Consultancy Services● MediumFULLTIMEOA

Updated Aug 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report identifies merge-sort inversion counting. Strict duplicate handling, bounds, and a 64-bit return are registered practice assumptions.

Given an integer array nums, return the number of inversions in it.

An inversion is a pair of indices (i, j) such that i < j and nums[i] > nums[j]. Equal values do not form an inversion.

Function

countInversions(nums: int[]) → long

Examples

Example 1

nums = [2,4,1,3,5]return = 3

The inverted pairs are (2,1), (4,1), and (4,3).

Example 2

nums = [5,4,3,2,1]return = 10

Every pair is inverted in a length-five descending array.

Constraints

  • 1 <= nums.length <= 200000
  • -2^31 <= nums[i] <= 2^31 - 1
  • The answer fits in a signed 64-bit integer.

More Tata Consultancy Services Online Assessment questions

  • EV Charging Cost OptimizationOA · Seen Sep 2026
  • Maximum Gap After SortingOA · Seen Sep 2026
  • Count Smaller Prior ElementsOA · Seen Aug 2026
  • Array Rank TransformOA · Seen Jul 2026
  • Gym Fees CalculatorOA · Seen Mar 2026

Similar questions at other companies

  • API Rate Limit Violation DetectionTekion · OATekion interview questions
  • Count Lamps Lighting Each PointAirbnb · OAAirbnb interview questions
  • Top K Frequent ElementsAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
  • Merge IntervalsApple · OA · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
  • Earliest Meeting Time for All EmployeesCapital One · OACapital One interview questions
  • Capping Portfolio WeightsCapstone Investment Advisors · OA
  • Count Servers With No Requests in a Time WindowIBM · OAIBM interview questions
  • Maximum Escape Game ScoreMicrosoft · OAMicrosoft interview questions
  • Largest Team Where Every Pair OverlapsWalmart · OAWalmart interview questions
  • Maximum Distinct Values After Unit AdjustmentsBirla Pivot · OA
All Tata Consultancy Services interview questionsSee Tata Consultancy Services hiring insights
Saved
class Solution {
    public long countInversions(int[] nums) {
        // Write your code here.
    }
}
Checking account…