Start learning free

LESSON 90 OF 410 · FREE CODING INTERVIEW QUESTIONS

Minimum Value of Three (Time Complexity: O(1), Language: JavaScript)

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 is at most B and A is at most 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 is at most B and A is at most C: return A
if B is at most A and B is at most 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 is at most B and A is at most C: return A
if B is at most A and B is at most C: return B
otherwise: 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 is less than minVal: minVal = B
if C is less than 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

    JAVASCRIPT · FREE CODING INTERVIEW QUESTIONS

    Minimum Value Of Three in JavaScript, explained

    Understanding the Problem

    The core challenge of this problem is to determine the smallest of three given integers without using built-in functions like Math.min. This is a fundamental problem that helps in understanding basic conditional logic and comparison operations.

    Common applications of this problem include scenarios where you need to find the minimum value in a set of numbers, which is a frequent requirement in algorithms and data processing.

    Potential pitfalls include not correctly handling the comparisons or missing edge cases where two or more numbers are equal.

    Approach

    To solve this problem, we can use simple conditional statements to compare the three numbers. Here’s a step-by-step approach:

    1. First, compare A with B and C. If A is less than or equal to both, return A.
    2. If the first condition is not met, compare B with A and C. If B is less than or equal to both, return B.
    3. If neither of the above conditions is met, return C as it must be the smallest.

    This approach ensures that we check all possibilities and return the correct minimum value.

    Algorithm

    Here’s a step-by-step breakdown of the algorithm:

    1. Initialize a variable minVal with the value of A.
    2. Compare B with minVal. If B is smaller, update minVal to B.
    3. Compare C with minVal. If C is smaller, update minVal to C.
    4. Return minVal.

    Code Implementation

    Here is the JavaScript code for the solution:

    // Function to find the minimum of three numbers
    function findMinimum(A, B, C) {
        // Initialize minVal with A
        let minVal = A;
        
        // Compare B with minVal
        if (B < minVal) {
            minVal = B;
        }
        
        // Compare C with minVal
        if (C < minVal) {
            minVal = C;
        }
        
        // Return the minimum value
        return minVal;
    }
    
    // Example usage:
    console.log(findMinimum(7, 4, 9)); // Output: 4
    console.log(findMinimum(3, 8, 3)); // Output: 3
    

    Complexity Analysis

    The time complexity of this solution is O(1) because we are performing a constant number of comparisons regardless of the input size. The space complexity is also O(1) as we are using a fixed amount of extra space.

    Edge Cases

    Potential edge cases include:

    • All three numbers are the same (e.g., A = 5, B = 5, C = 5). The function should return 5.
    • Two numbers are the same and smaller than the third (e.g., A = 2, B = 2, C = 3). The function should return 2.

    These cases are handled correctly by the algorithm as it checks all conditions systematically.

    Testing

    To test the solution comprehensively, consider the following test cases:

    • Simple cases with distinct numbers.
    • Cases where two or more numbers are the same.
    • Cases with negative numbers.

    Example test cases:

    console.log(findMinimum(7, 4, 9)); // Output: 4
    console.log(findMinimum(3, 8, 3)); // Output: 3
    console.log(findMinimum(-1, -5, -3)); // Output: -5
    console.log(findMinimum(0, 0, 0)); // Output: 0
    console.log(findMinimum(1, 2, 1)); // Output: 1
    

    Thinking and Problem-Solving Tips

    When approaching such problems, consider the following tips:

    • Break down the problem into smaller, manageable parts.
    • Think about all possible scenarios and edge cases.
    • Write pseudo-code before actual implementation to clarify your thought process.
    • Practice similar problems to improve your problem-solving skills.

    Conclusion

    In this blog post, we discussed how to find the minimum of three numbers without using built-in functions. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for developing strong problem-solving skills in programming.

    Keep practicing and exploring further to enhance your skills!

    Additional Resources

    For further reading and practice problems, consider the following resources:

    Keep going with JavaScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free