Problem overview
The function minValue receives three integers, A, B and C, and returns the smallest of them. The task forbids built-in helpers such as min, so the comparison has to be written by hand with if statements.
In the first example, A = 7, B = 4 and C = 9, so the answer is 4. In the second, A = 3, B = 8 and C = 3. The smallest value, 3, appears twice, and the function returns 3. Ties are not a problem: the function returns a value, not a position.
Approach: keep the smallest value seen so far
One way to solve it is to test each candidate: A is the answer if it is no larger than both others, otherwise B is the answer if it is no larger than A and C, and if neither holds, it must be C. That works, but it needs long conditions, and it gets much worse with four or five numbers.
This lesson uses a simpler idea that scales. Keep a variable, minVal, that holds the smallest value seen so far. Start it at A, since at that point A is the only number you have looked at. Then look at B: if it is smaller than minVal, it becomes the new minVal. Do the same with C. When you run out of numbers, minVal holds the answer.
Take A = 7, B = 4, C = 9. minVal starts at 7. Since 4 is less than 7, it becomes 4. Since 9 is not less than 4, it stays. The function returns 4.
Step by step
- Declare
minValwithvarand set it toA. Kotlin infers the typeIntfromA, so no type annotation is needed. It has to bevar, notval, because later steps may change it. - Write an
ifthat checksB < minVal. Leave its body empty for now. - Update
minValtoBinside theif. The comparison is strict, so an equal value doesn't replace the current one. That is harmless, because equal values are interchangeable. - Add a second
ifforC. It checksC < minValand assignsminVal = C. It must be a separateifplaced after the first one, not anelsebranch:Chas to be compared whether or notBreplaced the minimum. - Return
minVal. After both checks it holds the smallest of the three values.
The solution in Kotlin
class Solution {
fun minValue(A: Int, B: Int, C: Int): Int {
var minVal = A
if (B < minVal) {
minVal = B
}
if (C < minVal) {
minVal = C
}
return minVal
}
}
A few Kotlin details are worth knowing:
varandval. Avalis read-only: once set, it can't be reassigned, and the compiler reports an error if you try.minValchanges during the function, so it is avar.- Parameters are read-only. You can't write
A = Binside the function. That is why the code copiesAinto a new variable instead of updatingAitself. - Type inference.
var minVal = AgivesminValthe typeInt. After that, it can only hold anInt. - The standard library has the answer ready. Kotlin provides
minOf(A, B, C), which does the same job in one call. The lesson forbids it so you practise the comparison itself. - Parentheses around conditions. Kotlin requires them:
if (B < minVal). The braces around the body are optional for a single statement, but keeping them makes it easy to add a line later.
Edge cases and common mistakes
- Equal values.
minValue(3, 8, 3)returns3. Because the comparisons are strict,Cdoesn't replace aminValthat is already 3, and the result is the same either way. - Negative numbers.
minValue(-5, -5, -9)returns-9. StartingminValatAworks for any inputs. Starting it at0instead would be a mistake: forminValue(5, 8, 3)nothing is less than 0, so the function would return0, which isn't one of the inputs. - Extreme values.
minValue(Int.MIN_VALUE, 0, Int.MAX_VALUE)returns-2147483648. Only comparisons are used, and no arithmetic, so there is no overflow. - Using
elsefor the second check. IfC < minValsits in anelsebranch of the firstif,Cis never compared whenBwas smaller thanA. WithA = 7,B = 4andC = 1, the function would return4instead of1. - Using
valforminVal. The assignmentminVal = Bis then a compile error:'val' cannot be reassigned.
Testing
minValue(7, 4, 9)gives4.minValue(3, 8, 3)gives3.minValue(-5, -5, -9)gives-9.minValue(7, 4, 1)gives1.