Start learning free

LESSON 77 OF 338 · CONDITIONAL STATEMENTS

Allow to Contest in Go: Checking an Inclusive Range with &&

Practice Read the full explanation

At a contest, children can participate only if they have ages between minAge and maxAge. Given the age of a child, check if they are allowed or not.

Example 1:

Input: minAge = 7, maxAge = 12, age =  7
Output: true

Example 2:

Input: minAge = 3, maxAge = 8, age = 10
Output: false

Hints:

You can solve it in 3 different ways:
1. By checking for invalidations and return false when age < minAge and when age > maxAge, otherwise return true

2. Using one if statement with 2 conditions and return true, otherwise false

3. Since the function return value is a boolean, instead of using an if, you just return the condition since its value is a boolean.
STEP 1 OF 1
    Ctrl + Enter to check

    GO · CONDITIONAL STATEMENTS

    Allow To Contest in Go, explained

    Problem overview

    The function allowToContest takes three integers: minAge, maxAge and age. It returns true when the child's age lies between the two limits, and false otherwise. Both limits are allowed values.

    In the first example the limits are 7 and 12 and the child is 7. That is exactly the lower limit, so the answer is true. In the second example the limits are 3 and 8 and the child is 10, which is above the upper limit, so the answer is false.

    Approach

    An age is acceptable when two things hold at once: it is at least minAge, and it is at most maxAge. There are three ways to write that, all correct:

    1. Check for failures. Return false if age < minAge, return false if age > maxAge, and otherwise return true.
    2. Use one if with both conditions joined by &&, returning true or false from its branches.
    3. Skip the if. The joined condition is already a bool, so return it directly.

    The lesson takes the third route. Its idea is the same one as in the previous lesson on boolean expressions: a function that answers a yes-or-no question can return the condition itself.

    Step by step

    1. Write the lower-bound test. minAge <= age is true when the child is old enough. Both ends are inclusive, so the operator is <=, not <.
    2. Write the upper-bound test. age <= maxAge is true when the child is not too old.
    3. Join them with &&, which is true only when both sides are true, and return the result.

    Writing the two tests so the age sits between the limits, minAge <= age && age <= maxAge, mirrors the mathematical range minAge <= age <= maxAge and makes the intent easy to see.

    The solution in Go

    func allowToContest(minAge int, maxAge int, age int) bool {
        return minAge <= age && age <= maxAge
    }
    

    Go details worth knowing:

    • No chained comparison. Go can't evaluate minAge <= age <= maxAge. It would compare a boolean with a number, and the compiler refuses. Two comparisons joined by && are required.
    • Precedence. Comparison operators bind tighter than &&, so each side is evaluated first and then combined, with no parentheses.
    • Short-circuiting. If minAge <= age is false, Go does not evaluate the second comparison, because the result is already false. Here this only saves a tiny amount of work.
    • Parameter grouping. The signature can also be written (minAge, maxAge, age int), which means the same thing. The lesson repeats int for each parameter.

    Edge cases and common mistakes

    • The boundaries. Ages equal to minAge or maxAge are allowed. Using < would reject them.
    • Using || instead of &&. Every age is either at least the minimum or at most the maximum, so the function would almost always return true.
    • One-value range. If minAge equals maxAge, only that exact age passes.
    • Reversed limits. If minAge is greater than maxAge, no age can satisfy both tests, so the function always returns false. The lesson doesn't ask you to handle this.
    • Negative or zero ages. The function does no validation. It compares the three numbers as given, so allowToContest(0, 5, 0) is true and a negative age is simply below any non-negative minimum.
    • Mixing up the parameter order. The arguments come as minAge, maxAge, age. Swapping the last two would test the wrong value.

    Testing

    • allowToContest(7, 12, 7) returns true.
    • allowToContest(3, 8, 10) returns false.
    • allowToContest(7, 12, 12) returns true, the upper boundary.
    • allowToContest(7, 12, 6) returns false.
    • allowToContest(5, 5, 5) returns true.
    • allowToContest(9, 4, 6) returns false.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free