Start learning free

LESSON 242 OF 291 · HELPER FUNCTIONS

Max of Three with a Helper in Scala

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

    SCALA · HELPER FUNCTIONS

    Max Of Three Helper in Scala, explained

    Problem overview

    The lesson has two methods in one class. maxOfTwo(a, b) is a helper that returns the larger of two integers. maxOfThree(a, b, c) returns the largest of three integers, and it must get its answer by calling the helper instead of comparing numbers itself. Built-in max functions are off limits.

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

    Approach: build the answer from a smaller one

    Finding the larger of two numbers is easy. Finding the largest of three reduces to that: take the larger of the first two, then the larger of that winner and the third. The largest of all three can't lose either match.

    Splitting the work this way keeps each method small. The comparison is written once, in maxOfTwo, and maxOfThree only combines calls. The same pattern extends to four numbers or more without any new comparing code.

    Step by step

    1. In maxOfTwo, return a when it is greater than b. return ends the method immediately, so nothing below runs in that case.
    2. Return b below the if. This line is reached only when a is not greater than b. Either b is larger, or they are equal and b is just as good. No else is needed.
    3. In maxOfThree, nest the calls. The inner call maxOfTwo(a, b) runs first, and its result becomes the first argument of the outer call, which compares it with c. A method calls another method of the same class by name, with no object in front.

    The solution in Scala

    class Solution {
        def maxOfTwo(a: Int, b: Int): Int = {
            if (a > b) {
                return a
            }
            return b
        }
    
        def maxOfThree(a: Int, b: Int, c: Int): Int = {
            return maxOfTwo(maxOfTwo(a, b), c)
        }
    }
    

    Scala details worth knowing:

    • Declared return types. A method that uses return must state its result type, here : Int, after the parameter list.
    • Same parameter names, separate variables. Both methods have parameters called a and b. Each method has its own variables, so the call maxOfTwo(a, b) just hands over the current values.
    • if is an expression. Scala could write the helper as if (a > b) a else b, which is the value of the method with no return at all. The lesson uses the early-return form, which is the one most beginners know from other languages.

    Edge cases and common mistakes

    • Ties. With maxOfTwo(5, 5) the condition a > b is false, so the helper returns b, which is 5. The answer is right because both values are equal.
    • Negative numbers. Nothing special happens: -1 > -8 is true. Starting a "largest so far" at 0 would be the real bug, because it would return 0 for all-negative input.
    • Comparing in maxOfThree. Adding your own if there defeats the point of the lesson. All comparing lives in the helper.
    • Calling the helper with the wrong values. maxOfTwo(a, c) alone would ignore b. The nested call must pass the winner of the first match.

    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.
    • maxOfTwo(5, 5) gives 5.
    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free