Problem overview
Given three integers a, b and c, return the largest. The lesson splits the work between two functions in the same class. The helper maxOfTwo takes two numbers and returns the larger one. The function that is checked, maxOfThree, must find the largest of the three by calling the helper. It never compares numbers itself, and built-in max functions are off limits.
For 3, 7, 5 the answer is 7. For -3, -8, -1 it is -1, the negative number closest to zero. For 4, 4, 2 it is 4: when two numbers tie, either one is the answer because the value is the same.
Approach
Finding the larger of two numbers is a single comparison. Finding the largest of three is two such comparisons chained together: the winner of a against b then meets c. Writing the pairwise comparison once, in a helper, means maxOfThree only has to arrange the calls.
Step by step
The helper. Inside maxOfTwo, if a > b the function returns a straight away. A return ends the function on the spot, so nothing below it runs in that case.
The next line returns b. The function only reaches it when a is not greater than b, which means b is larger or the two are equal, and in both cases b is a correct answer. That is why no else is needed.
The main function. maxOfThree returns maxOfTwo(maxOfTwo(a, b), c). Inside a class, one function calls another by its name, so there is no object or prefix to write. The inner call runs first and gives the larger of a and b. That result becomes the first argument of the outer call, which compares it with c.
The solution in Kotlin
class Solution {
fun maxOfTwo(a: Int, b: Int): Int {
if (a > b) {
return a
}
return b
}
fun maxOfThree(a: Int, b: Int, c: Int): Int {
return maxOfTwo(maxOfTwo(a, b), c)
}
}
Tracing maxOfThree(3, 7, 5): the inner call maxOfTwo(3, 7) returns 7, then maxOfTwo(7, 5) returns 7. For maxOfThree(1, 2, 9) the inner call returns 2, and maxOfTwo(2, 9) returns 9, so the largest value can be in any of the three positions.
Some Kotlin details:
- Every function declares its parameter types and its return type, written after the colon:
: Int. - A function that has a block body and declares a return type must return on every path. Both
returnstatements in the helper make sure of that. - The parameters of the two functions share the names
aandb, but each function has its own variables. The callmaxOfTwo(maxOfTwo(a, b), c)passes values, so the helper'saandbare separate frommaxOfThree's.
Edge cases and common mistakes
- Negative numbers. A common mistake is to start from 0 as "the best so far". With
-3, -8, -1that would return 0, which isn't one of the inputs. The pairwise comparison never invents a value. - Equal numbers. With
a == bthe conditiona > bis false, so the helper returnsb, which has the same value asa.maxOfTwo(5, 5)returns 5. - Comparing inside
maxOfThree. Addingifchecks there defeats the lesson's idea. The helper should do all the comparing. - Wrong nesting.
maxOfTwo(a, b, c)doesn't compile, because the helper takes exactly two arguments. The calls have to be nested.
Testing
maxOfThree(3, 7, 5)returns 7.maxOfThree(-3, -8, -1)returns -1.maxOfThree(4, 4, 2)returns 4.maxOfThree(1, 2, 9)returns 9.
