Start learning free

LESSON 241 OF 289 · HELPER FUNCTIONS

Max of Three with a Helper in C#

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

    C# · HELPER FUNCTIONS

    Max Of Three Helper in C#, explained

    Problem overview

    The class has two methods. MaxOfTwo(int a, int b) is a helper: it returns the larger of two numbers. MaxOfThree(int a, int b, int c) returns the largest of three, and it must get there by calling the helper rather than comparing numbers itself. Built-in max methods are off limits, so all comparing happens in one place.

    For 3, 7 and 5 the answer is 7. For -3, -8 and -1 it is -1, the negative number closest to zero. For 4, 4 and 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 it: the largest of a, b and c is the larger of (the larger of a and b) and c. If the winner of the first pair is beaten by c, c is the maximum. Otherwise the winner of the first pair is. Either way one more call to the helper settles it.

    Splitting the work this way means each method does one small job. MaxOfTwo knows how to compare. MaxOfThree only knows how to chain two comparisons.

    Step by step

    1. In MaxOfTwo, return a when a > b. return ends the method on the spot, so when a is larger nothing after it runs.
    2. Return b below the if. The method only reaches this line when a is not greater than b. Then b is larger, or they are equal and b is just as right. No else is needed, because the first return already left the method.
    3. In MaxOfThree, nest one call inside another. A method calls another method of the same class by its name. MaxOfTwo(a, b) runs first and gives the larger of the pair. That value becomes the first argument of the outer call, which compares it with c. There is no if in MaxOfThree.

    The solution in C#

    class Solution {
        public int MaxOfTwo(int a, int b) {
            if (a > b) {
                return a;
            }
            return b;
        }
    
        public int MaxOfThree(int a, int b, int c) {
            return MaxOfTwo(MaxOfTwo(a, b), c);
        }
    }
    

    Things worth knowing:

    • Each method has its own variables. MaxOfTwo names its parameters a and b, and so does MaxOfThree, but they are separate. When MaxOfThree calls MaxOfTwo(MaxOfTwo(a, b), c), the outer call's a receives the inner result and its b receives c.
    • Arguments are evaluated before the call. The inner MaxOfTwo(a, b) finishes first, and only then does the outer call start.
    • Ties are fine. With a == b the condition a > b is false, so the helper returns b, which equals a.

    Edge cases and common mistakes

    • The maximum in any position. MaxOfThree(1, 2, 9) and MaxOfThree(9, 2, 1) both return 9, because the chain checks every number once.
    • Negative numbers. MaxOfThree(-3, -8, -1) returns -1. Starting a "best so far" at 0 would wrongly return 0 here, which is why the helper compares the actual inputs.
    • Writing the comparison twice. Putting an if in MaxOfThree works but defeats the point of the helper: if the rule ever changes, you would have to change it in two places.
    • Forgetting return. A C# method with an int return type must return a value on every path, or the code doesn't compile.

    Testing

    These calls give these results:

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