Start learning free

LESSON 132 OF 348 · LOOPS & CONDITIONALS

Hurdle Jump in Kotlin: Checking Every Hurdle with a 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:

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

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

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

hurdleJump([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

    KOTLIN · LOOPS & CONDITIONALS

    Hurdle Jump in Kotlin, explained

    Problem overview

    The function canJump receives an IntArray of hurdle heights, hurdleHeights, and the jumper's height, jumperHeight. It returns true if the jumper can clear every hurdle and false if any hurdle is too tall. A hurdle is cleared when the jump height is greater than or equal to the hurdle height, so a jump of 5 clears a hurdle of height 5.

    Take hurdles [5, 5, 3, 4, 5] and a jump height of 3. The first hurdle has height 5, which is above 3, so the answer is false. With hurdles [1, 2, 3, 4, 5] and a jump height of 5, every hurdle is at most 5, so the answer is true. An empty array of hurdles also gives true: with nothing to jump over, any height is enough.

    Approach: reject on the first hurdle that is too tall

    This is the mirror image of searching an array for a value. The jumper clears the course only if every hurdle passes the test, and a single failure is enough to answer false. So the code looks for a counterexample:

    • If some hurdle is taller than jumperHeight, return false immediately. The other hurdles can't change that.
    • If the loop ends without finding such a hurdle, every hurdle passed, so return true.

    Be careful with the comparison. A hurdle exactly as tall as the jump height is cleared, so the test for failure is a strict "taller than", not "taller than or equal to".

    Step by step

    1. Loop over the hurdles. Use a for loop with a variable named height so each hurdle is inspected in order. The values are all that matter, so no index is needed.
    2. Return false for a hurdle that is too tall. If height is greater than jumperHeight, the answer is settled. The return leaves the function, and the remaining hurdles are skipped.
    3. Return true after the loop. Getting there means no hurdle was too tall. It also covers the empty array, where the loop body never runs.

    The solution in Kotlin

    class Solution {
        fun canJump(hurdleHeights: IntArray, jumperHeight: Int): Boolean {
            for (height in hurdleHeights) {
                if (height > jumperHeight) {
                    return false
                }
            }
            return true
        }
    }
    

    Kotlin notes:

    • Return type and paths. The function is declared to return a Boolean, so the compiler requires every path to end with a value. The final return true provides it for the case where the loop finishes.
    • Looping directly over an array. for (height in hurdleHeights) gives each element in order. The loop variable is read-only inside the body.
    • Comparison operators. > works on Int values. Writing >= by mistake would reject a hurdle exactly as tall as the jump, which the task says is clearable.
    • A shorter form exists. Kotlin has a built-in all function for arrays, which expresses "every element passes this test". The loop shows what it does underneath.

    Complexity

    Time: O(n), where n is the number of hurdles. In the worst case, when every hurdle can be cleared or only the last one is too tall, each hurdle is compared once. If the first hurdle is too tall, the function stops after one comparison.

    Space: O(1). Only the loop variable is stored.

    Edge cases and common mistakes

    • Empty array. No hurdles means the loop is skipped and the function returns true, as the task requires.
    • Hurdle equal to the jump height. [1, 2, 1] with a jump of 1 returns false because of the hurdle of height 2. But [1, 1] with a jump of 1 returns true, since equal heights can be cleared.
    • Returning true inside the loop. Writing if (height <= jumperHeight) return true would stop at the first clearable hurdle. For [1, 9] and a jump of 3 it would answer true after seeing the 1, ignoring the 9.
    • Checking only the first or last hurdle. The tallest hurdle can be anywhere. Every element must be examined unless a failure ends the search earlier.
    • Negative values. They aren't expected, but the code handles them like any other number.

    Testing

    • hurdleHeights = [1, 2, 3, 4, 5], jumperHeight = 5 gives true.
    • hurdleHeights = [5, 5, 3, 4, 5], jumperHeight = 3 gives false.
    • hurdleHeights = [5, 4, 5, 6], jumperHeight = 10 gives true.
    • hurdleHeights = [1, 2, 1], jumperHeight = 1 gives false.
    • hurdleHeights = [], jumperHeight = 1 gives true.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free