Start learning free

LESSON 87 OF 415 · FREE CODING INTERVIEW QUESTIONS

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

    SWIFT · FREE CODING INTERVIEW QUESTIONS

    Minimum Value Of Three in Swift, explained

    Problem overview

    The method minValue in class Solution takes three integers, A, B and C, and returns the smallest of them. The task forbids built-ins such as 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, returning either copy gives the same number.

    Approach: keep the best value seen so far

    One way is to ask of each number, "is this one less than or equal to both others?", and return the first that passes. It works, but the conditions grow with every extra value.

    A more scalable idea is to carry a running answer. Start by assuming A is the minimum. Look at B: if it is smaller than the current answer, it replaces it. Look at C and do the same. After the last check, the variable holds the smallest value, because it was compared with every other number along the way. Adding a fourth number would mean adding one more if, and the same pattern later becomes a loop over an array.

    Step by step

    1. Declare minVal with var and set it to A. A var can be reassigned, and the next steps will do exactly that. A let constant could not be.
    2. Write an if that tests B < minVal. Swift needs no parentheses around the condition, but the braces are required.
    3. Assign B to minVal inside it. The test is strict: a value equal to the current minimum changes nothing, so no assignment is needed.
    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 be smaller than both.
    5. Return minVal. After both checks it holds the smallest of the three.

    The solution in Swift

    class Solution {
        func minValue(_ A: Int, _ B: Int, _ C: Int) -> Int {
            var minVal = A
            if B < minVal {
                minVal = B
            }
            if C < minVal {
                minVal = C
            }
            return minVal
        }
    }
    

    Swift details worth knowing:

    • The underscores. Each parameter is written _ A: Int. The underscore removes the argument label, so a call reads minValue(7, 4, 9). Without it, the call would have to be minValue(A: 7, B: 4, C: 9).
    • Type inference. var minVal = A needs no type: A is an Int, so minVal is too.
    • Parameters are constants. Inside the method, A, B and C cannot be assigned to. That is why the code copies A into a new variable instead of overwriting it.
    • Conditions must be Bool. if B alone, or a number used as a condition, is a compile error; Swift does not treat 0 as false.

    Calling it:

    let solution = Solution()
    print(solution.minValue(7, 4, 9))
    print(solution.minValue(3, 8, 3))
    print(solution.minValue(-1, -5, -3))
    
    4
    3
    -5
    

    Complexity

    Time: O(1). The method always does two comparisons, no matter what the values are. Space: O(1). It uses a single extra variable.

    Edge cases and common mistakes

    • Equal values. minValue(2, 2, 2) returns 2. Since the comparisons are strict, no assignment happens, and the starting value is already right.
    • Starting from a made-up value. Initialising minVal to 0 instead of A would return 0 for (4, 5, 6), a number that is not among the inputs. Starting from one of the inputs avoids that.
    • Using else if for C. For (5, 3, 1), B replaces A in the first if, the else if branch is skipped, and the method returns 3 instead of 1.
    • Using let for minVal. The compiler rejects the later assignment with an error that says the value is a let constant.
    • Calling the global min. Swift has a built-in min function, and it would work, but this lesson asks you to write the comparison yourself.

    Testing

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