Start learning free

LESSON 245 OF 293 · HELPER FUNCTIONS

Max of Three with a Helper in Rust

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

    RUST · HELPER FUNCTIONS

    Max Of Three Helper in Rust, explained

    Problem overview

    This lesson has two functions. max_of_two is a helper that takes two integers and returns the larger one. max_of_three takes three integers and returns the largest, and it must do that by calling the helper rather than comparing numbers itself. For a = 3, b = 7, c = 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 gives the same value.

    Approach: build the answer from a smaller one

    A helper is a small function that does one job so another function can reuse it. Here the job is "which of these two is larger?". The largest of three numbers is the larger of two things: the winner of the first pair, and the third number. So the plan is to compare a and b, then compare that winner with c. Both comparisons are the same job, so one helper covers both.

    Step by step

    1. Return a when it is greater. Inside max_of_two, if a > b returns a immediately. A return ends the function on the spot.
    2. Return b otherwise. The line after the if only runs when a is not greater than b. Then b is larger, or the two are equal, and in both cases b is a correct answer. No else is needed.
    3. Nest the calls in max_of_three. Self::max_of_two(a, b) runs first and gives the larger of the first two. That value becomes the first argument of the outer call, which compares it with c.

    The solution in Rust

    impl Solution {
        fn max_of_two(a: i32, b: i32) -> i32 {
            if a > b {
                return a;
            }
            return b;
        }
    
        fn max_of_three(a: i32, b: i32, c: i32) -> i32 {
            return Self::max_of_two(Self::max_of_two(a, b), c);
        }
    }
    

    Tracing a = 3, b = 7, c = 5: the inner call max_of_two(3, 7) returns 7, then the outer call max_of_two(7, 5) returns 7. The function returns 7.

    Rust details worth knowing:

    • Calling a function of the same impl. These functions take no self, so they are associated functions, and a call needs Self:: in front of the name. Writing plain max_of_two(a, b) fails with cannot find function `max_of_two` in this scope, and the compiler suggests Self::.
    • A call is an expression. Self::max_of_two(a, b) has a value, an i32, so it can sit directly inside another call's argument list.
    • The helper's types. Both parameters and the return value are i32, so the nested call type-checks without any conversion.

    Why use a helper at all

    The helper keeps each function small enough to read at a glance. max_of_two has one decision in it, and max_of_three has none of its own: it only says how the pieces fit together. The same helper would also work for four numbers, by nesting one more call, with no new comparison logic. When a bug shows up, there is exactly one place where numbers are compared, so there is exactly one place to look.

    Edge cases and common mistakes

    • Ties. With a = 4 and b = 4, a > b is false, so the helper returns b. It is the same number as a, so the result is right.
    • Negative numbers. Comparison works the same way for them: -1 is greater than -3 and -8.
    • The largest in any position. The result is 9 for (1, 2, 9) and for (9, 2, 1). Because every number takes part in one of the two comparisons, it does not matter where the largest one sits.
    • Skipping the helper. Writing the comparisons directly inside max_of_three may give correct answers, but the lesson asks for the helper to do all the comparing.

    Testing

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