Kotlinlearncs.online LogoJava
Return to List

Test Writing: Array Is Peak

Created By: Pranav Narayanan
/ Version: 2025.2.0

Write a function isPeak that takes an IntArray? and returns true if and only if it is a peak. An array is a peak if it consists of a single run of increasing numbers joined with a single run of decreasing numbers. For example {1, 2, 1} is a peak, since it has the increasing run {1, 2} joined with the decreasing run {2, 1}.

The runs need not be strictly increasing or strictly decreasing. For example, {1, 1, 2, 2, 1, 1} is a peak, since it consists of {1, 1, 2, 2} joined with {2, 2, 1, 1}. However, there must be at least one increase and one decrease in the array for it to qualify. {1, 1, 2}, {2, 1, 1}, and {1, 1, 1} should all fail under this condition.

Notice that these conditions require arrays of at least length 3. If your array is shorter than this or null, return false.

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-is-peak/[email protected]