Start learning free

LESSON 238 OF 287 · HELPER FUNCTIONS

Max of Three with a Helper in Python

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

    PYTHON · HELPER FUNCTIONS

    Max Of Three Helper in Python, explained

    Problem overview

    Given three integers a, b and c, return the largest. The twist is the structure: the class has two methods. maxOfTwo is a helper that returns the larger of two numbers. maxOfThree must find the largest of three by calling the helper, without comparing numbers itself, and without the built-in max.

    For a = 3, b = 7, c = 5 the answer is 7. For a = -3, b = -8, c = -1 it is -1, the negative number closest to zero. For a = 4, b = 4, c = 2 it is 4: when two numbers tie, either one is the answer, because the value is the same.

    Approach: split the job into a small piece and reuse it

    Finding the larger of two numbers is a tiny problem. Finding the largest of three reduces to it: the largest of a, b and c is the larger of "the larger of a and b" and c. Once the helper exists, the three-number function is a single expression.

    That is the point of a helper function: write the comparison once, test it on its own, and let bigger functions build on it.

    Step by step

    1. In maxOfTwo, return a if a > b. return ends the function immediately, so nothing after it runs when a wins.
    2. Otherwise return b. The code only reaches this line when a is not greater than b: either b is larger or they are equal, and b is right in both cases. No else is needed.
    3. In maxOfThree, call the helper twice, one call inside the other. The inner call, self.maxOfTwo(a, b), runs first. Its result becomes the first argument of the outer call, which compares it with c.

    The solution in Python

    class Solution:
        def maxOfTwo(self, a: int, b: int) -> int:
            if a > b:
                return a
            return b
    
        def maxOfThree(self, a: int, b: int, c: int) -> int:
            return self.maxOfTwo(self.maxOfTwo(a, b), c)
    

    Details worth knowing:

    • self. is required. Inside a class, one method calls another through the instance, as self.maxOfTwo(...). Writing just maxOfTwo(a, b) raises a NameError, because the method is not a plain function in scope.
    • Nested calls evaluate from the inside out. With a = 3, b = 7, c = 5, the inner call returns 7, and the outer call becomes self.maxOfTwo(7, 5), which returns 7.
    • Parameters are local. The names a and b in maxOfTwo are separate from the ones in maxOfThree. Each call gets its own copies of the values it was handed.

    Tracing the nested call

    Take a = 3, b = 7, c = 5. The inner call self.maxOfTwo(3, 7) returns 7. The outer call is then self.maxOfTwo(7, 5), which returns 7. The answer is 7.

    Now a = -3, b = -8, c = -1. The inner call returns -3, since -3 is greater than -8. The outer call compares -3 with -1 and returns -1. In both traces the first call reduces three numbers to two, and the second reduces two to one.

    The same idea extends to any number of values. For four numbers, maxOfTwo(maxOfTwo(maxOfTwo(2, 9), 4), 6) returns 9: each call folds one more value into the running winner. A loop would be the natural way to write that for a long list, but for three numbers the nested call is short and clear.

    Edge cases and common mistakes

    • Ties. With a = 4, b = 4, the test a > b is false, so b is returned. It equals a, so the answer is correct.
    • Negative numbers. Comparisons work the same way: -1 is greater than -3 and -8, so maxOfThree(-3, -8, -1) returns -1.
    • Comparing in maxOfThree. Adding your own if there defeats the exercise. The helper does all the comparing.
    • Ignoring the result. Calling self.maxOfTwo(a, b) without using what it returns throws the answer away. The inner call must feed the outer one.

    Testing

    • maxOfThree(3, 7, 5) gives 7.
    • maxOfThree(-3, -8, -1) gives -1.
    • maxOfThree(4, 4, 2) gives 4.
    • maxOfThree(1, 2, 3) gives 3, and maxOfThree(9, 1, 1) gives 9: the largest can be in any position.
    • maxOfTwo(5, 5) gives 5.
    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free