Start learning free

LESSON 86 OF 348 · FREE CODING INTERVIEW QUESTIONS

Minimum Value of Three in Kotlin: Compare and Keep the Smallest

Practice Read the full explanation

Given 3 integers A, B and C, return the smallest number.

You are not allowed to use built-in functions such as min

Example 1:

Input: A = 7, B = 4, C = 9
Output: 4

Example 2:

Input: A = 3, B = 8, C = 3
Output: 3


There are three candidates to the solution: A, B and C. We should try out each of them until we find the correct one. How can we check if A is the solution? What should happen for A to be the minimum value?

A is the minimum value if A <= B and A <= C. We can write an if statement checking these conditions and if they hold, return A:
if A <= B and A <= C:
	return A
What if the conditions don't hold and A is not the solution? What should we do next?

We should test B. Similarly, B is the minimum value if B <= A and B <= C. We can write an if statement checking these conditions and if they hold, return B:
if A <= B and A <= C:
	return A
if B <= A and B <= C:
	return B
What if not even B is the solution?

Then, it's clear that C is the solution, so we should return C. We don't even need to check the conditions anymore:
if A <= B and A <= C:
	return A
if B <= A and B <= C:
	return B
return C


While the solution above is very good, we propose another one which is more scalable (i.e. works better for a larger number of values). It involves going throught the numbers and using a variable minVal to store the minimum one found so far:
minVal = A
if B < minVal:
	minVal = B
if C < minVal:
	minVal = C
return minVal
Please watch the video solution for a better understanding of this solution.

STEP 1 OF 5
    Ctrl + Enter to check

    KOTLIN · FREE CODING INTERVIEW QUESTIONS

    Minimum Value Of Three in Kotlin, explained

    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

    1. Declare minVal with var and set it to A. Kotlin infers the type Int from A, so no type annotation is needed. It has to be var, not val, because later steps may change it.
    2. Write an if that checks B < minVal. Leave its body empty for now.
    3. Update minVal to B inside the if. The comparison is strict, so an equal value doesn't replace the current one. That is harmless, because equal values are interchangeable.
    4. Add a second if for C. It checks C < minVal and assigns minVal = C. It must be a separate if placed after the first one, not an else branch: C has to be compared whether or not B replaced the minimum.
    5. 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:

    • var and val. A val is read-only: once set, it can't be reassigned, and the compiler reports an error if you try. minVal changes during the function, so it is a var.
    • Parameters are read-only. You can't write A = B inside the function. That is why the code copies A into a new variable instead of updating A itself.
    • Type inference. var minVal = A gives minVal the type Int. After that, it can only hold an Int.
    • 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) returns 3. Because the comparisons are strict, C doesn't replace a minVal that is already 3, and the result is the same either way.
    • Negative numbers. minValue(-5, -5, -9) returns -9. Starting minVal at A works for any inputs. Starting it at 0 instead would be a mistake: for minValue(5, 8, 3) nothing is less than 0, so the function would return 0, 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 else for the second check. If C < minVal sits in an else branch of the first if, C is never compared when B was smaller than A. With A = 7, B = 4 and C = 1, the function would return 4 instead of 1.
    • Using val for minVal. The assignment minVal = B is then a compile error: 'val' cannot be reassigned.

    Testing

    • minValue(7, 4, 9) gives 4.
    • minValue(3, 8, 3) gives 3.
    • minValue(-5, -5, -9) gives -9.
    • minValue(7, 4, 1) gives 1.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free