Start learning free

LESSON 76 OF 348 · CONDITIONAL STATEMENTS

Allow To Contest in Kotlin: Range Check 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

    KOTLIN · CONDITIONAL STATEMENTS

    Allow To Contest in Kotlin, explained

    Problem overview

    A contest accepts children only within an age range. The function allowToContest(minAge: Int, maxAge: Int, age: Int): Boolean receives the lowest and highest allowed ages and the age of one child. It returns true if the child may take part and false otherwise.

    Both ends of the range are included, as the examples show:

    • minAge = 7, maxAge = 12, age = 7 returns true. A child aged exactly 7 is allowed.
    • minAge = 3, maxAge = 8, age = 10 returns false, because 10 is above 8.

    Approach

    A child is allowed when two things hold at once: the age is not below minAge, and it is not above maxAge. There are three ways to turn that into code, and they all give the same answers.

    1. Reject, then accept. Return false if age < minAge, return false if age > maxAge, and return true at the end.
    2. One if with two conditions. Return true inside an if that joins both checks with &&, and false otherwise.
    3. Return the condition. The joined condition is already a Boolean, so the function can return it directly.

    The lesson uses the third form. It is the shortest, and it has no branches that can disagree with the condition.

    Step by step

    1. Write the lower bound. "Not below the minimum" is minAge <= age. The <= includes the boundary, so a child of exactly minAge passes.
    2. Write the upper bound. "Not above the maximum" is age <= maxAge. Again the boundary is included.
    3. Join them. Use &&, which is true only when both sides are. Return that whole expression.

    Written with the three values in order, the condition reads like the maths for a range: minimum, then age, then maximum.

    The solution in Kotlin

    class Solution {
        fun allowToContest(minAge: Int, maxAge: Int, age: Int): Boolean {
            return minAge <= age && age <= maxAge
        }
    }
    

    Kotlin details:

    • Each side of && is a full comparison. You can't write the maths shorthand minAge <= age <= maxAge. Kotlin evaluates the first comparison into a Boolean and then fails to compare that with maxAge, so the compiler reports an error.
    • Precedence. Comparisons bind tighter than &&, so no parentheses are needed.
    • Short-circuiting. If minAge <= age is false, the right side is not evaluated, since the result is already false.
    • Range alternative. Kotlin also offers age in minAge..maxAge, which is inclusive at both ends and means the same thing here.

    Tracing Example 1: 7 <= 7 is true and 7 <= 12 is true, so the result is true. For Example 2: 3 <= 10 is true but 10 <= 8 is false, so the result is false.

    The long forms stay useful when the reasons for rejecting differ and you want to treat them separately, for example to tell the child whether they are too young or too old. When you only need a yes or no, the single expression is the clearest choice.

    Edge cases and common mistakes

    • The boundaries. Ages equal to minAge or maxAge are allowed. Using < instead of <= would turn those children away. Test both ends.
    • Using || instead of &&. With "or", every age passes when the range is valid, because any age is either at least minAge or at most maxAge. The check would never reject anyone.
    • Mixing up the parameters. The signature takes minAge first, then maxAge, then age. Swapping the last two changes the meaning of the whole condition.
    • A reversed range. If minAge is larger than maxAge, no age can satisfy both checks, so the function returns false for every child.
    • A range of one age. When minAge equals maxAge, only that exact age returns true.

    Testing

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