Kotlinlearncs.online LogoJava
Return to List

Test Writing: Array Count Inversions

Created By: Pranav Narayanan
/ Version: 2025.3.0

Declare and implement a function called countArrayInversions that receives an array of Int values and returns a count of the number of inversions in the array. An inversion is a pair of indices i and j where i < j but A[i] > A[j], for the array A.

Note: this can be completed with a nested for loop, but a faster algorithm can be achieved using recursion and a modified merge sort. If you choose to do this, note that you cannot modify the input array, so you may need to copy it. You may also want to write a private helper function, like so:

Test Design Challenge

You're challenge is to write tests for this problem described above.

  • Provide a method named test that accepts no arguments and does not return a value.
  • If the implementation of the class described above is incorrect, your test method should throw an exception.
  • If it is correct, do not throw an exception.
  • You may want to use Kotlin's assert or check methods
Report a Problem

Attribution must link to this page: https://www.learncs.online/testtesting/kotlin/array-count-inversions/[email protected]