Start learning free

LESSON 90 OF 410 · FREE CODING INTERVIEW QUESTIONS

Minimum Value of Three in Python

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

    PYTHON · FREE CODING INTERVIEW QUESTIONS

    Minimum Value Of Three in Python, explained

    Problem Definition

    The problem requires us to find the smallest of three given integers A, B, and C without using built-in functions like min. The input consists of three integers, and the output should be the smallest of these integers.

    Input:

    • Three integers A, B, and C.

    Output:

    • The smallest integer among A, B, and C.

    Constraints:

    • All inputs are integers.

    Example:

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

    Understanding the Problem

    The core challenge is to determine the smallest of three numbers without using built-in functions. This problem is fundamental in understanding basic conditional statements and comparisons. It has applications in various fields such as sorting algorithms, decision-making processes, and more.

    Potential Pitfalls:

    • Incorrectly comparing the numbers.
    • Not considering all possible cases.

    Approach

    To solve this problem, we can use conditional statements to compare the three numbers. We will start by comparing A with B and C, then B with A and C, and finally, if neither A nor B is the smallest, C will be the smallest by default.

    Naive Solution:

    The naive solution involves directly comparing the numbers using if-else statements. This approach is straightforward but not scalable for a larger number of values.

    Optimized Solution:

    An optimized approach involves using a variable to keep track of the minimum value found so far. This method is more scalable and can be easily extended to more than three numbers.

    Algorithm

    Step-by-Step Breakdown:

    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 as the smallest number.

    Code Implementation

    def find_minimum(A, B, C):
        # Initialize minVal with A
        minVal = A
        
        # Compare B with minVal
        if B < minVal:
            minVal = B
        
        # Compare C with minVal
        if C < minVal:
            minVal = C
        
        # Return the smallest value
        return minVal
    
    # Test cases
    print(find_minimum(7, 4, 9))  # Output: 4
    print(find_minimum(3, 8, 3))  # Output: 3
    

    Complexity Analysis

    The time complexity of this approach 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

    Consider the following edge cases:

    • All three numbers are the same: A = 5, B = 5, C = 5. The output should be 5.
    • Two numbers are the same and the smallest: A = 2, B = 2, C = 3. The output should be 2.
    • Negative numbers: A = -1, B = -2, C = -3. The output should be -3.

    Testing

    To test the solution comprehensively, consider a variety of test cases:

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

    Thinking and Problem-Solving Tips

    When approaching such problems, consider the following tips:

    • Break down the problem into smaller parts.
    • Use conditional statements effectively.
    • Think about edge cases and how to handle them.

    Conclusion

    In this blog post, we discussed how to find the minimum of three numbers without using built-in functions. We explored a naive solution and an optimized approach, provided a detailed algorithm, and analyzed the complexity. Understanding such fundamental problems is crucial for developing problem-solving skills and preparing for more complex challenges.

    Additional Resources

    For further reading and practice, consider the following resources:

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