Start learning free

LESSON 137 OF 414 · LOOPS & CONDITIONALS

Hurdle Jump in Scala: Checking All Heights with a for Loop

Practice Read the full explanation

Given an array of hurdle heights and a jumper's jump height, determine whether or not the hurdler can clear all the hurdles. If they can, return true; otherwise return false.

A hurdler can clear a hurdle if their jump height is greater than or equal to the hurdle height.

Examples:

canJump([1, 2, 3, 4, 5], 5) ➞ true

canJump([5, 5, 3, 4, 5], 3) ➞ false

canJump([5, 4, 5, 6], 10) ➞ true

canJump([1, 2, 1], 1) ➞ false

Note: Return true for the edge case of an empty array of hurdles. (Zero hurdles means that any jump height can clear them).

STEP 1 OF 3
    Ctrl + Enter to check

    SCALA · LOOPS & CONDITIONALS

    Hurdle Jump in Scala, explained

    Problem overview

    The method canJump receives the heights of the hurdles as an Array[Int] named hurdleHeights, and the jumper's jump height as an Int named jumperHeight. It returns true if the jumper clears every hurdle and false if at least one hurdle is too tall.

    A hurdle is cleared when the jump height is greater than or equal to the hurdle's height. So a jumper with height 5 clears a hurdle of height 5. An empty array of hurdles counts as cleared, because there is nothing to fail on.

    Some examples with their results:

    • Hurdles [1, 2, 3, 4, 5], jump height 5: every hurdle is at most 5, so the result is true.
    • Hurdles [5, 5, 3, 4, 5], jump height 3: the first hurdle is 5, taller than 3, so the result is false.
    • Hurdles [5, 4, 5, 6], jump height 10: all hurdles are lower, so the result is true.
    • Hurdles [1, 2, 1], jump height 1: the hurdle of height 2 is too tall, so the result is false.

    Approach: look for one hurdle that fails

    "Clears all hurdles" is a statement about every element, and a single counterexample is enough to disprove it. That suggests the same early-exit pattern as a search: scan the hurdles, and the moment one is taller than the jump height, answer false. Only if the whole scan finishes without a failure can the answer be true.

    The logic is the mirror image of searching for a value. A search returns true from inside the loop and false after it. This problem returns false from inside the loop and true after it.

    Step by step

    1. Loop over the hurdles. for (height <- hurdleHeights) gives each hurdle height to the body as height.
    2. Detect a hurdle that is too tall. The test is height > jumperHeight, with a strict >. A hurdle exactly as tall as the jump can be cleared, so equality must not count as a failure. When the test is true, return false immediately.
    3. Return true after the loop. Reaching this line means no hurdle failed the test. For an empty array the loop body never runs, so the method returns true, which is what the task wants.

    The solution in Scala

    class Solution {
        def canJump(hurdleHeights: Array[Int], jumperHeight: Int): Boolean = {
            for (height <- hurdleHeights) {
                if (height > jumperHeight) {
                    return false
                }
            }
            return true
        }
    }
    

    Some Scala details:

    • The loop variable is a name you choose. height is a fresh value on every pass, taken from the array in index order.
    • return leaves the whole method. It doesn't just end the current pass, so the remaining hurdles are skipped once one has failed.
    • A deprecation warning. Scala 3.5 compiles this with a warning that a non-local return, meaning a return inside a for body, is no longer supported and suggests boundary.break. The method still works as described.
    • The method name. The code calls the method canJump, so that is the name to use when you call it, as in new Solution().canJump(Array(5, 4, 5, 6), 10).
    • A one-line version. hurdleHeights.forall(_ <= jumperHeight) expresses the same rule. It is also true for an empty array.

    Complexity

    Time: O(n), where n is the number of hurdles. In the worst case, every hurdle is checked once. When an early hurdle fails, the loop stops sooner.

    Space: O(1). The method uses only the loop variable.

    Edge cases and common mistakes

    • No hurdles. canJump(Array(), 0) is true, since the loop finds nothing to reject.
    • Equal heights. A hurdle of height 4 and a jump of 4 is cleared, so canJump(Array(4), 4) is true. Writing height >= jumperHeight would wrongly reject it.
    • The failing hurdle is last. For [1, 2, 9] with a jump of 5, the first two hurdles pass and only the last one fails. The method must not decide before the loop has seen it.
    • Returning true inside the loop. A return true in an else branch would answer after the first hurdle, which is wrong for [1, 9] with a jump of 5.
    • Swapped comparison. Using jumperHeight > height as the failure test reverses the meaning, because that is the condition for a hurdle being cleared.

    Testing

    • canJump(Array(1, 2, 3, 4, 5), 5) gives true.
    • canJump(Array(5, 5, 3, 4, 5), 3) gives false.
    • canJump(Array(5, 4, 5, 6), 10) gives true.
    • canJump(Array(1, 2, 1), 1) gives false.
    • canJump(Array(), 0) gives true.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free