Start learning free

LESSON 242 OF 285 · HELPER FUNCTIONS

Max of Three with a Helper Function in Go

Practice Read the full explanation

Given three integers a, b and c, return the largest of them.

This time the work is split between two functions. The first one is a helper: it gets two numbers and returns the larger one. The second one, the function that is graded, finds the largest of the three numbers by calling the helper, so it never compares numbers itself.

Don't use a built-in max function: the helper does the comparing. The numbers can be negative, and some of them can be equal.

 

Example 1:

Input: a = 3, b = 7, c = 5
Output: 7
Explanation: The larger of 3 and 7 is 7, and the larger of 7 and 5 is still 7.

Example 2:

Input: a = -3, b = -8, c = -1
Output: -1
Explanation: All three numbers are negative. -1 is the closest to zero, so it is the largest.

Example 3:

Input: a = 4, b = 4, c = 2
Output: 4
Explanation: a and b tie for the largest. Either one is the answer, because the value is the same: 4.

 

Constraints:

  • a, b and c are integers between -1000000 and 1000000.
  • Two or all three of them can be equal.
STEP 1 OF 3
    Ctrl + Enter to check

    GO · HELPER FUNCTIONS

    Max Of Three Helper in Go, explained

    Problem overview

    Given three integers a, b and c, return the largest. The lesson splits the work between two functions. The helper maxOfTwo takes two numbers and returns the larger one. The graded function maxOfThree must find the largest of three by calling the helper, without comparing numbers itself.

    For a = 3, b = 7, c = 5 the answer is 7. For a = -3, b = -8, c = -1 the answer is -1, the negative number closest to zero. For a = 4, b = 4, c = 2 the answer is 4: when two values tie, either one is the answer because they are equal.

    Approach: solve for two, then reuse it

    Finding the larger of two numbers is a small problem with a small solution. Finding the largest of three reduces to it: the largest of three is the larger of "the larger of the first two" and the third number. If the helper already answers the two-number question, maxOfThree only has to call it twice.

    This is the main reason to write helper functions: give a small job a name, get it right once, and build on it. The same helper would extend to four or five numbers by nesting more calls.

    Step by step

    1. In maxOfTwo, return a when a > b. A return ends the function immediately, so nothing after it runs for that case.
    2. Return b below the if. The function only reaches this line when a is not greater than b. Then either b is larger or the two are equal, and b is correct in both cases. No else is needed.
    3. In maxOfThree, return one call inside another: maxOfTwo(maxOfTwo(a, b), c). The inner call runs first and gives the larger of a and b. That result becomes the first argument of the outer call, which compares it with c.

    The solution in Go

    func maxOfTwo(a int, b int) int {
        if a > b {
            return a
        }
        return b
    }
    
    func maxOfThree(a int, b int, c int) int {
        return maxOfTwo(maxOfTwo(a, b), c)
    }
    

    A few Go details:

    • The return type comes after the parameters. In func maxOfTwo(a int, b int) int, the last int is the type of the value the function returns.
    • Each function has its own variables. Both functions have parameters named a and b. They are separate: when maxOfThree calls maxOfTwo(a, b), the helper receives copies of the values.
    • A function that returns a value must return on every path. If maxOfTwo ended after the if without the final return b, the compiler would report a missing return.
    • Call order. Go evaluates the arguments of a call before the call itself, so the inner maxOfTwo(a, b) is finished before the outer one starts.

    Edge cases and common mistakes

    • Negative numbers. Starting a "largest so far" at 0 would be wrong for -3, -8, -1, which would give 0. The helper only compares the inputs, so it is correct for any sign.
    • Ties. With a = 4 and b = 4, a > b is false, so the helper returns b, which is 4. Using >= would also work.
    • Position of the maximum. The largest value can be the first, the middle or the last argument. For (9, 1, 2), (3, 7, 5) and (1, 2, 9) the results are 9, 7 and 9.
    • Comparing in the wrong function. The lesson wants maxOfThree to rely on the helper alone, with no if of its own.

    Testing

    • a = 3, b = 7, c = 5 gives 7.
    • a = -3, b = -8, c = -1 gives -1.
    • a = 4, b = 4, c = 2 gives 4.
    • a = 9, b = 1, c = 2 gives 9.
    • a = 1, b = 2, c = 9 gives 9.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free