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 = 7returnstrue. A child aged exactly 7 is allowed.minAge = 3,maxAge = 8,age = 10returnsfalse, 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.
- Reject, then accept. Return
falseifage < minAge, returnfalseifage > maxAge, and returntrueat the end. - One
ifwith two conditions. Returntrueinside anifthat joins both checks with&&, andfalseotherwise. - 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
- Write the lower bound. "Not below the minimum" is
minAge <= age. The<=includes the boundary, so a child of exactlyminAgepasses. - Write the upper bound. "Not above the maximum" is
age <= maxAge. Again the boundary is included. - Join them. Use
&&, which istrueonly 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 shorthandminAge <= age <= maxAge. Kotlin evaluates the first comparison into aBooleanand then fails to compare that withmaxAge, so the compiler reports an error. - Precedence. Comparisons bind tighter than
&&, so no parentheses are needed. - Short-circuiting. If
minAge <= ageisfalse, the right side is not evaluated, since the result is alreadyfalse. - 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
minAgeormaxAgeare 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 leastminAgeor at mostmaxAge. The check would never reject anyone. - Mixing up the parameters. The signature takes
minAgefirst, thenmaxAge, thenage. Swapping the last two changes the meaning of the whole condition. - A reversed range. If
minAgeis larger thanmaxAge, no age can satisfy both checks, so the function returnsfalsefor every child. - A range of one age. When
minAgeequalsmaxAge, only that exact age returnstrue.
Testing
allowToContest(7, 12, 7)returnstrue.allowToContest(3, 8, 10)returnsfalse.allowToContest(3, 8, 8)returnstrue.allowToContest(8, 3, 5)returnsfalse.