Start learning free

LESSON 87 OF 414 · FREE CODING INTERVIEW QUESTIONS

Minimum Value of Three in Scala: Tracking a Running Minimum

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 && 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 && A <= C) {
	return A
}
if (B <= A && 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 && A <= C) {
	return A
}
if (B <= A && 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:
var 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

    SCALA · FREE CODING INTERVIEW QUESTIONS

    Minimum Value Of Three in Scala, explained

    Problem overview

    The method minValue receives three Int values, A, B and C, and returns the smallest of them. The lesson forbids built-in helpers such as min, so the comparison has to be written by hand.

    With A = 7, B = 4 and C = 9 the answer is 4. With A = 3, B = 8 and C = 3 the answer is 3: the smallest value may appear more than once, and the method returns the value, not a position.

    Approach: keep the best value seen so far

    There are two natural ways to solve this. The first asks of each number, "is it less than or equal to both of the others?" and returns the first one for which the answer is yes. It works, but every extra number adds more conditions to every check.

    The lesson uses the second way, which scales better. Keep one variable, minVal, that holds the smallest value seen so far. Start it at A, since after looking at one number that number is the minimum. Then look at the next number: if it is smaller than minVal, it becomes the new minVal. Repeat for the third. Each new number costs one comparison, and the same pattern handles four numbers or four thousand.

    Step by step

    1. Declare minVal with var and start it at A. Scala infers the type Int from A, so no annotation is needed. It must be a var because it will be reassigned; a val would make the later assignments a compile error.
    2. Add an if that tests B < minVal. Put spaces around the <. Written as B <minVal, the compiler reads <minVal as the start of an XML literal and reports ',' or ')' expected, but $XMLSTART$< found, which has nothing to do with what you meant.
    3. Inside it, assign minVal = B. The test is strictly "less than", so an equal value changes nothing, which is fine because the result would be the same.
    4. Add a second, separate if for C < minVal and assign minVal = C in it. It must be a separate if, not an else: 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.

    The solution in Scala

    class Solution {
        def 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 Scala details are worth knowing:

    • return is optional. In Scala the last expression of a block is its value, so ending the method with just minVal on its own line works the same way, and that is the more common style. The explicit return is legal and does the same thing here.
    • The return type is written after the parameters. ): Int = declares that the method gives back an Int, and the body in braces follows the =.
    • Parameters are values. A, B and C can't be reassigned inside the method, which is why the lesson copies A into a separate var.
    • Capital parameter names are legal. Scala style usually starts parameter names in lower case, but the lesson's names are A, B and C, and the method works with them.
    • An if without else is fine here. Used as a statement, it simply does nothing when its condition is false.

    Edge cases and common mistakes

    • Ties. minValue(3, 8, 3) returns 3. With < the second 3 doesn't replace the first, and with <= it would replace it with an identical value, so both comparisons give the correct answer.
    • All values equal. minValue(5, 5, 5) returns 5, and no assignment happens at all.
    • Negative numbers and extremes. minValue(-1, -7, -3) returns -7, and minValue(Int.MinValue, 0, 3) returns -2147483648. Only comparisons are used, so nothing can overflow.
    • Starting at 0. Initializing minVal to 0 instead of A breaks every case where all three numbers are positive, because nothing is smaller than 0 and the method would return 0. Start from a value that is actually one of the inputs.
    • Using else if for the second check. If B replaced minVal, the C comparison would be skipped. For minValue(9, 8, 1) that would return 8 instead of 1.
    • Declaring minVal with val. The compiler rejects the later assignments with a reassignment-to-val error.

    Testing

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