Start learning free

LESSON 91 OF 348 · FREE CODING INTERVIEW QUESTIONS

Minimum Value of Three in TypeScript: 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

    TYPESCRIPT · FREE CODING INTERVIEW QUESTIONS

    Minimum Value Of Three in TypeScript, explained

    Problem overview

    The method minValue receives three numbers, A, B and C, and returns the smallest of them. The lesson 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. The second example shows that the smallest value may appear twice, and the method still returns it once.

    Approach: keep the best answer found so far

    One way to solve this is to ask, for each value, whether it is less than or equal to both of the others. If A passes that test, return it. If not, test B. If neither passes, C must be the smallest. That works, but the conditions get longer with every value you add.

    This lesson uses a scalable idea instead. Keep a variable minVal that holds the smallest value seen so far. Start it at A, because with only one value looked at, that value is the minimum. Then look at each remaining value in turn. If it is smaller than minVal, it replaces it. When every value has been looked at, minVal is the answer.

    Here is the idea on 7, 4, 9. minVal starts as 7. Since 4 is less than 7, minVal becomes 4. Since 9 is not less than 4, minVal stays 4. The method returns 4.

    The same pattern handles an array of any length: replace the two if statements with a loop. Later lessons rely on it.

    Step by step

    1. Declare minVal with let, typed number, and set it to A. Use let because the variable will change. A const would make the later assignments a compile error.
    2. Write an if that checks whether B is less than minVal. It starts empty so you can focus on the condition.
    3. Inside it, assign B to minVal. The comparison is strict (<), because an equal value changes nothing.
    4. Add a second, separate if for C. It must not be an else of the first one. C has to be compared whether or not B took over, since C may beat both.
    5. Return minVal. After both checks, it holds the smallest of the three.

    The solution in TypeScript

    class Solution {
        minValue(A: number, B: number, C: number): number {
            let minVal: number = A;
            if (B < minVal) {
                minVal = B;
            }
            if (C < minVal) {
                minVal = C;
            }
            return minVal;
        }
    }
    

    Some details worth knowing:

    • number covers every numeric value. TypeScript has one number type for integers and decimals, so the method works for -1.5 or 0.25 as well as for whole numbers.
    • The annotation on minVal is optional. TypeScript infers number from A. Writing it out is just a way of being explicit.
    • Types disappear at run time. The compiler checks that you pass numbers, but the generated JavaScript has no such check. A caller from plain JavaScript could still pass strings, and then < would compare them as text.

    Edge cases and common mistakes

    • Using else if for the second check. If B replaced minVal, the C check would be skipped. minValue(5, 3, 1) would then return 3 instead of 1.
    • Starting minVal at 0. The result would be wrong whenever all three values are positive, because 0 would beat them all. Start from a real input value.
    • All negative numbers. minValue(-1, -5, -3) returns -5. The comparisons work the same way for negatives.
    • Equal values. With 3, 8, 3, nothing is smaller than 3, so minVal stays at A and the result is 3.

    Testing

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