Start learning free

LESSON 85 OF 338 · FREE CODING INTERVIEW QUESTIONS

Minimum Value of Three in Go: Track the Smallest So Far

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

    GO · FREE CODING INTERVIEW QUESTIONS

    Minimum Value Of Three in Go, explained

    Problem overview

    The function minValue receives three integers, A, B and C, and returns the smallest of them. Built-in helpers such as min are off limits, so the comparison has to be written by hand.

    For A = 7, B = 4, C = 9 the answer is 4. For A = 3, B = 8, C = 3 the answer is 3: when the smallest value appears twice, it is still just that value, so ties need no special handling.

    Approach

    There are two natural ways to solve this.

    The first checks each candidate directly. A is the minimum when it is no larger than the other two, so you test A <= B && A <= C, then the same for B, and whatever is left must be C. It works, but every extra number adds more conditions to every test.

    The second keeps one variable, minVal, that holds the smallest value seen so far. It starts as A. Each further value is compared against minVal only, and replaces it when it is smaller. After the last value, minVal is the answer. This lesson uses this version, because adding a fourth or a hundredth number means adding one more comparison of the same shape instead of rewriting the conditions.

    Take 7, 4, 9. minVal starts at 7. Next, 4 is smaller than 7, so minVal becomes 4. Then 9 is not smaller than 4, so nothing changes. The function returns 4.

    Step by step

    1. Declare minVal with :=, initialised to A. Until a smaller value shows up, A is the best answer, and starting from a real input value avoids guessing a "large enough" starting number.
    2. Write an if that checks B < minVal. The body can stay empty for the moment.
    3. Update inside it: minVal = B. The comparison is strict, because an equal value would change nothing anyway.
    4. Repeat for C, with a second if after the first. It has to be a separate if, not an else: C must be compared whether or not B replaced the minimum.
    5. Return minVal. By now it has been compared with all three values.

    The solution in Go

    func minValue(A int, B int, C int) int {
        minVal := A
        if B < minVal {
            minVal = B
        }
        if C < minVal {
            minVal = C
        }
        return minVal
    }
    

    A few Go details are worth knowing:

    • := declares and infers the type. minVal := A creates a new variable of type int, because A is an int. Later lines use plain = to assign to it. Writing := again for the same name in the same scope is a compile error ("no new variables on left side of :=").
    • No parentheses around conditions. Go writes if B < minVal {. The braces are mandatory, even for a one-line body, and the opening brace must be on the same line as the if.
    • Parameters of the same type can share it. func minValue(A, B, C int) int means the same as the longer form above. The lesson spells out each type, and both are valid.
    • Newer Go has a built-in min. Since Go 1.21 you can write min(A, B, C). The lesson forbids it so that you practise the comparison logic yourself.

    Complexity

    Time: O(1). There are exactly two comparisons, however large the numbers are. Space: O(1). The function stores one extra integer.

    Edge cases and common mistakes

    • A made-up starting value. Starting minVal at 0 would return 0 for minValue(7, 4, 9), because no input is smaller than 0. Starting from A keeps the answer one of the real inputs. Negative inputs work the same way: minValue(-5, -5, -9) returns -9.
    • Using else for the second check. With A = 5, B = 3, C = 1, an else would skip the C comparison after B replaced the minimum, and the function would return 3 instead of 1.
    • Equal values. minValue(2, 2, 2) returns 2. Using <= instead of < would also work but performs pointless assignments.
    • Forgetting to return. A Go function with a result type must end with a return, or the code doesn't compile ("missing return").

    Testing

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