Start learning free

LESSON 78 OF 414 · CONDITIONAL STATEMENTS

Allow to Contest in Scala: Checking a 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

    SCALA · CONDITIONAL STATEMENTS

    Allow To Contest in Scala, explained

    Problem overview

    The method allowToContest receives 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 inclusive.

    In the first example, minAge = 7, maxAge = 12 and age = 7. The child is exactly at the lower limit, which counts as allowed, so the answer is true. In the second example, minAge = 3, maxAge = 8 and age = 10. Ten is above the upper limit, so the answer is false.

    Approach: one condition, returned directly

    An age is allowed when two things hold at once: it is at least minAge and it is at most maxAge. The "and" is the logical AND operator, &&, which is true only when both of its sides are true.

    There are three ways to write this, and they all do the same work:

    1. Reject the bad cases first: return false if age < minAge, return false if age > maxAge, and return true after that.
    2. Use one if with both conditions joined by &&, returning true in the branch and false after it.
    3. Skip the if entirely. A comparison such as age <= maxAge already is a Boolean, and so is the result of &&, so the condition itself is the answer.

    This lesson uses the third form. An if that returns true in one branch and false in the other only repeats the value of its own condition.

    Step by step

    The lesson has a single step, but it contains two decisions.

    1. Write the lower bound as minAge <= age. It is true when the child is old enough, including the case where the age equals minAge.
    2. Write the upper bound as age <= maxAge. It is true when the child is not too old, again including equality.
    3. Join them with && and return the whole expression. Reading minAge <= age && age <= maxAge from left to right gives the range minAge <= age <= maxAge.

    Using < instead of <= is the usual slip. With <, the first example would return false for age 7, because 7 is not strictly greater than 7.

    The solution in Scala

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

    A few Scala details are worth knowing here:

    • Chaining does not work. Scala has no chained comparison like minAge <= age <= maxAge. The first comparison produces a Boolean, which then meets maxAge, an Int, and the compiler reports a type mismatch. Two comparisons joined by && are required.
    • Precedence. The comparison operators bind tighter than &&, so no parentheses are needed around either comparison.
    • Short-circuiting. && evaluates its right side only when the left side is true. For plain integer comparisons this makes no visible difference, but it matters when the right side has a side effect or could fail.
    • The return keyword. The last expression of a method body is its result, so the return in the lesson's code is optional. Writing just minAge <= age && age <= maxAge inside the braces returns the same value. Scala programmers usually leave return out.
    • The declared types. The signature promises a Boolean, and the expression has that type, so no conversion is needed.

    Edge cases and common mistakes

    • The limits themselves. allowToContest(3, 8, 8) returns true, and allowToContest(3, 8, 3) returns true too. Both ends are inclusive.
    • Using || instead of &&. With OR, minAge <= age || age <= maxAge is true for nearly every age, because any age fails at most one of the two checks. The second example would wrongly return true.
    • Swapped limits. If minAge is larger than maxAge, no age can satisfy both conditions, so the method returns false for every age. allowToContest(8, 3, 5) gives false.
    • Redundant branches. Code such as if (cond) true else false works but says the same thing as cond in more words.

    Testing

    • minAge = 7, maxAge = 12, age = 7 gives true.
    • minAge = 3, maxAge = 8, age = 10 gives false.
    • minAge = 3, maxAge = 8, age = 8 gives true.
    • minAge = 3, maxAge = 8, age = 2 gives false.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free