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
- Declare
minValwith:=, initialised toA. Until a smaller value shows up,Ais the best answer, and starting from a real input value avoids guessing a "large enough" starting number. - Write an
ifthat checksB < minVal. The body can stay empty for the moment. - Update inside it:
minVal = B. The comparison is strict, because an equal value would change nothing anyway. - Repeat for
C, with a secondifafter the first. It has to be a separateif, not anelse:Cmust be compared whether or notBreplaced the minimum. - 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 := Acreates a new variable of typeint, becauseAis anint. 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 theif. - Parameters of the same type can share it.
func minValue(A, B, C int) intmeans 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 writemin(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
minValat 0 would return 0 forminValue(7, 4, 9), because no input is smaller than 0. Starting fromAkeeps the answer one of the real inputs. Negative inputs work the same way:minValue(-5, -5, -9)returns-9. - Using
elsefor the second check. WithA = 5,B = 3,C = 1, anelsewould skip theCcomparison afterBreplaced the minimum, and the function would return 3 instead of 1. - Equal values.
minValue(2, 2, 2)returns2. 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)gives4.minValue(3, 8, 3)gives3.minValue(-5, -5, -9)gives-9.minValue(2, 2, 2)gives2.