Kotlinlearncs.online LogoJava
Return to List

Test Writing: Array All Factors

Created By: Pranav Narayanan
/ Version: 2025.1.0

Write a method named arrayAllFactors that, given an array of Int values greater than 0, returns whether the values are all factors of one of the values in the array, which we'll call the greatest multiple. For example, given the array {4, 2, 8} you would return true, since 2 and 4 are factors of the greatest multiple 8. Given the array {2, 2, 5} you would return false, since there is no value that all others are factors of.

You should approach this problem in two steps. First, identify the greatest multiple—the value in the array that you are going to check whether others are factors of. This may sound complicated, but it's straightforward. You do not need to and should not check whether every value is the base! Your solution should not contain a nested loop.

Next, check all the values in the array and look for a counterexample—a value that is not a factor of the base.

The passed array may be null or empty. In those cases you should return false. Valid arrays will contain only integers strictly greater than 0.

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-all-factors/[email protected]