Start learning free

LESSON 243 OF 292 · HELPER FUNCTIONS

Max of Three with a Helper in Swift

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

    SWIFT · HELPER FUNCTIONS

    Max Of Three Helper in Swift, explained

    Problem overview

    The class has two methods. maxOfTwo is a helper: it receives two integers and returns the larger one. maxOfThree receives three integers, a, b and c, and returns the largest, using the helper for every comparison. Swift's built-in max is not used.

    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, since the value is the same.

    Approach

    Finding the largest of three numbers is two steps of finding the larger of two. The larger of a and b is a candidate; the larger of that candidate and c is the winner. Writing the comparison once, in maxOfTwo, and calling it twice is the point of a helper function: the logic lives in one place, and maxOfThree reads as a plan rather than a pile of if statements.

    The two calls nest. The inner one, maxOfTwo(a, b), runs first, and its result becomes the first argument of the outer call.

    Tracing one call

    Take maxOfThree(3, 7, 5). The inner call maxOfTwo(3, 7) compares 3 with 7, finds that 3 is not greater, and returns 7. The expression becomes maxOfTwo(7, 5), where 7 is greater than 5, so the final answer is 7. For maxOfThree(-3, -8, -1) the inner call returns -3, and the outer call then compares -3 with -1 and returns -1.

    The same idea extends to any number of values. Four numbers would be maxOfTwo(maxOfThree(a, b, c), d): each new function builds on the one before it, and no comparison is ever written twice.

    Step by step

    1. In maxOfTwo, return a when a > b. return ends the function immediately, so nothing after it runs for that case.
    2. Return b below the if. The function only gets there when a is not greater than b: b is larger, or the two are equal and either is correct. No else is needed.
    3. In maxOfThree, nest the calls. Return maxOfTwo(maxOfTwo(a, b), c).

    The solution in Swift

    class Solution {
        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)
        }
    }
    

    Swift details worth knowing:

    • Calling a sibling method. Inside a class, a method calls another method of the same class by its bare name. self.maxOfTwo(a, b) means the same thing, and Swift allows leaving out self here.
    • No labels in the calls. Each parameter is declared with a leading _, so the arguments are passed without names. Without the underscores, the calls would have to read maxOfTwo(a: a, b: b).
    • Parameters are constants. a and b inside maxOfTwo are separate from the a and b of maxOfThree. Each call receives copies of the values, and neither function can change the other's variables.

    Edge cases and common mistakes

    • Ties. With a = 4 and b = 4, the condition a > b is false and the helper returns b, which is 4. Using >= would also work.
    • Negative numbers. Nothing special is needed. A solution that starts from 0 as the "largest so far" would wrongly return 0 for -3, -8, -1; comparing the inputs with each other avoids that.
    • The largest in any position. Because every value takes part in a comparison, the answer is found whether it is first, second or third.
    • Comparing in maxOfThree. Writing if statements there goes against the lesson's design: all comparing happens in the helper.

    Testing

    • maxOfThree(3, 7, 5) gives 7.
    • maxOfThree(-3, -8, -1) gives -1.
    • maxOfThree(4, 4, 2) gives 4.
    • maxOfThree(9, 1, 2) gives 9, and maxOfThree(1, 2, 9) gives 9.
    Keep going with Swift1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free