Start learning free

LESSON 82 OF 410 · CONDITIONAL STATEMENTS

Allow to Contest - JavaScript Solution and Time Complexity Analysis

Practice Read the full explanation

At a contest, children can participate only if they have ages between the minimum age and the maximum age (both included). 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 is below the minimum age and when it is above the maximum age, 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

    JAVASCRIPT · CONDITIONAL STATEMENTS

    Allow To Contest in JavaScript, explained

    Understanding the Problem

    The core challenge of this problem is to determine if a given age falls within a specified range. This is a common problem in scenarios where eligibility criteria are based on age, such as contests, sports events, or age-restricted content.

    Potential pitfalls include misunderstanding the inclusive nature of the age range or incorrectly implementing the boundary checks.

    Approach

    To solve this problem, we need to check if the given age is within the inclusive range defined by minAge and maxAge. There are several ways to approach this:

    Naive Solution

    A naive solution would involve checking if the age is less than minAge or greater than maxAge and returning false in those cases. Otherwise, return true. This approach is straightforward but can be optimized.

    Optimized Solutions

    1. Using a single if statement with two conditions.

    2. Directly returning the boolean result of the condition check.

    Algorithm

    Let's break down the steps for each approach:

    Naive Approach

    function isAllowedToContest(minAge, maxAge, age) {
      // Check if age is less than minAge or greater than maxAge
      if (age < minAge || age > maxAge) {
        return false;
      }
      return true;
    }
    

    Optimized Approach 1

    function isAllowedToContest(minAge, maxAge, age) {
      // Use a single if statement with two conditions
      if (age >= minAge && age <= maxAge) {
        return true;
      }
      return false;
    }
    

    Optimized Approach 2

    function isAllowedToContest(minAge, maxAge, age) {
      // Directly return the boolean result of the condition check
      return age >= minAge && age <= maxAge;
    }
    

    Complexity Analysis

    All the approaches have a time complexity of O(1) because they involve a constant number of operations regardless of the input size. The space complexity is also O(1) as no additional space is required.

    Edge Cases

    Consider the following edge cases:

    • Age is exactly equal to minAge or maxAge.
    • Age is less than minAge or greater than maxAge.
    • Age is within the range but very close to the boundaries.

    Each algorithm handles these edge cases effectively by checking the inclusive range.

    Testing

    To test the solution comprehensively, consider the following test cases:

    console.log(isAllowedToContest(7, 12, 7)); // true
    console.log(isAllowedToContest(3, 8, 10)); // false
    console.log(isAllowedToContest(5, 10, 5)); // true
    console.log(isAllowedToContest(5, 10, 10)); // true
    console.log(isAllowedToContest(5, 10, 4)); // false
    console.log(isAllowedToContest(5, 10, 11)); // false
    

    Thinking and Problem-Solving Tips

    When approaching such problems, consider the following tips:

    • Understand the problem requirements and constraints thoroughly.
    • Break down the problem into smaller, manageable parts.
    • Consider edge cases and how your solution handles them.
    • Optimize your solution by reducing unnecessary checks or operations.

    Conclusion

    In this blog post, we discussed how to determine if a child's age falls within a specified range using JavaScript. We explored multiple approaches, analyzed their complexities, and provided comprehensive testing strategies. Understanding and solving such problems is crucial for developing strong problem-solving skills.

    Additional Resources

    For further reading and practice, consider the following resources:

    Keep going with JavaScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free