Start learning free

LESSON 84 OF 347 · FREE CODING INTERVIEW QUESTIONS

Minimum Value of Three in C#: Track the Smallest So Far

Practice Read the full explanation

Given 3 integers A, B and C, return the smallest number.

You are not allowed to use built-in functions such as min

Example 1:

Input: A = 7, B = 4, C = 9
Output: 4

Example 2:

Input: A = 3, B = 8, C = 3
Output: 3


There are three candidates to the solution: A, B and C. We should try out each of them until we find the correct one. How can we check if A is the solution? What should happen for A to be the minimum value?

A is the minimum value if A <= B and A <= C. We can write an if statement checking these conditions and if they hold, return A:
if A <= B and A <= C:
	return A
What if the conditions don't hold and A is not the solution? What should we do next?

We should test B. Similarly, B is the minimum value if B <= A and B <= C. We can write an if statement checking these conditions and if they hold, return B:
if A <= B and A <= C:
	return A
if B <= A and B <= C:
	return B
What if not even B is the solution?

Then, it's clear that C is the solution, so we should return C. We don't even need to check the conditions anymore:
if A <= B and A <= C:
	return A
if B <= A and B <= C:
	return B
return C


While the solution above is very good, we propose another one which is more scalable (i.e. works better for a larger number of values). It involves going throught the numbers and using a variable minVal to store the minimum one found so far:
minVal = A
if B < minVal:
	minVal = B
if C < minVal:
	minVal = C
return minVal
Please watch the video solution for a better understanding of this solution.

STEP 1 OF 5
    Ctrl + Enter to check

    C# · FREE CODING INTERVIEW QUESTIONS

    Minimum Value Of Three in C#, explained

    Problem overview

    The method MinValue receives three integers, A, B and C, and returns the smallest of them. The statement forbids built-in helpers such as Math.Min, so the comparison has to be written by hand.

    For A = 7, B = 4, C = 9 the answer is 4. For A = 3, B = 8, C = 3 the answer is 3: when the smallest value appears twice, you simply return that value.

    Approach

    One way is to ask, for each number, "is it less than or equal to both of the others?" and return the first one that says yes. That works, but every extra number makes the conditions longer.

    The lesson uses a simpler idea that scales to any count of values. Keep a variable, minVal, holding the smallest value seen so far. Start it at A, since after looking at one number that number is the minimum. Then look at each remaining number once, and replace minVal whenever you meet something smaller.

    On 7, 4, 9: minVal starts at 7. Next, 4 is less than 7, so minVal becomes 4. Then 9 is not less than 4, so nothing changes. The method returns 4.

    Step by step

    1. Declare minVal as an int and set it to A. This is the "best so far" after looking at the first number.
    2. Write an if that tests whether B is less than minVal. The body can stay empty for the moment.
    3. Assign B to minVal inside that if. Use a strict <: an equal value changes nothing, so there is no reason to replace it.
    4. Add a second, separate if for C. It compares C with the current minVal, which may already be B. If it were an else if, C would be skipped whenever B won, and the answer could be wrong.
    5. Return minVal. After both checks it holds the smallest of the three.

    The solution in C#

    class Solution {
        public int MinValue(int A, int B, int C) {
            int minVal = A;
            if (B < minVal) {
                minVal = B;
            }
            if (C < minVal) {
                minVal = C;
            }
            return minVal;
        }
    }
    

    A few C# details:

    • The type is declared. int minVal = A; states the type up front. C# is statically typed, so minVal can only ever hold an int.
    • The condition needs parentheses and must be a bool. if (B < minVal) is valid. C# does not treat numbers as true or false the way some languages do.
    • Parameters are copies. Assigning to minVal never changes A, B or C. Value types such as int are passed by value.
    • Every path returns. The method ends with return minVal; outside the if blocks, so the compiler is satisfied that all paths return an int.

    The library call the statement rules out is Math.Min(a, b), which takes two values. Taking the minimum of three would need two calls, Math.Min(A, Math.Min(B, C)). Writing the comparisons yourself shows what that call does.

    Edge cases and common mistakes

    • Ties. With 3, 8, 3, minVal starts at 3, 8 is not smaller, and 3 is not strictly smaller than 3, so the result stays 3. Using <= would give the same answer here.
    • Negative numbers. -5, -5, -9 returns -9. The comparisons work the same for negatives.
    • Extreme values. int.MinValue as A is never replaced, because nothing is smaller. Comparisons never overflow; only arithmetic does, and this method does none.
    • Using else if for C. For 5, 3, 1, B replaces minVal, and an else if would never test C, returning 3 instead of 1.
    • Starting minVal at 0. If all three numbers are positive, 0 would stay the "minimum" and the answer would be wrong. Start from one of the actual inputs.
    • Using = instead of <. if (minVal = B) does not compile in C#, because an assignment to an int is not a bool. That is one mistake the compiler catches for you.

    Testing

    • 7, 4, 9 gives 4.
    • 3, 8, 3 gives 3.
    • -5, -5, -9 gives -9.
    • int.MinValue, 0, 1 gives -2147483648.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free