Kotlinlearncs.online LogoJava
Return to List

Test Writing: Array Select Nth Smallest

Created By: Pranav Narayanan
/ Version: 2025.2.0

Write a public class Select that contains a single class method select. select takes an Integer array and and int value n, and returns the nth smallest value in the array. The array is guaranteed to be non-null with at least 1 element, and n will be positive and less than the length of the array.

You may NOT solve this problem by sorting the array - instead, you should use the partition method described here. You should import it using the following code:

The best algorithm for this is called QuickSelect, and it is described here.

You do not need a private helper function to solve this problem, but you are allowed to use one if you wish. Note that you may NOT modify the input array, so you may need to copy it.

Test Design Challenge

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

  • Provide a public class named TestSelect with a single non-private class 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 Java's assert method
Report a Problem

Attribution must link to this page: https://www.learncs.online/testtesting/java/array-select-nth-smallest/[email protected]