Start learning free

LESSON 132 OF 338 · LOOPS & CONDITIONALS

Hurdle Jump in Go: Checking Every Hurdle with range

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

    GO · LOOPS & CONDITIONALS

    Hurdle Jump in Go, explained

    Problem overview

    The function canJump receives a slice of hurdle heights, hurdleHeights, and the height the jumper can clear, 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 hurdle exactly as tall as the jump is fine.

    With hurdles [1, 2, 3, 4, 5] and a jump of 5, every hurdle is at most 5, so the answer is true. With hurdles [1, 2, 1] and a jump of 1, the middle hurdle is 2, which is higher than 1, so the answer is false. With no hurdles at all, the answer is true, because there is nothing to fail.

    Approach: one hurdle that is too tall is enough

    The jumper has to clear all hurdles, so a single failure settles the answer. Check the hurdles one by one. As soon as one is higher than jumperHeight, return false. If the loop finishes without finding such a hurdle, every one was clearable, so return true.

    This is the mirror image of searching for a value: there, one match means true; here, one violation means false. In both, the answer you can only give after looking at everything goes after the loop.

    Step by step

    1. Loop over the hurdles with range, calling each one height. The index is not needed, so it goes to the blank identifier _.
    2. Test for a failure. If height is strictly greater than jumperHeight, the jumper can't clear it, and the function returns false right away. The comparison is strict because a hurdle of equal height is still cleared.
    3. Return true after the loop. Reaching this line means no hurdle failed the test. An empty slice skips the loop and arrives here directly, which is the required answer for zero hurdles.

    The solution in Go

    func canJump(hurdleHeights []int, jumperHeight int) bool {
        for _, height := range hurdleHeights {
            if height > jumperHeight {
                return false
            }
        }
        return true
    }
    

    Some Go details to note:

    • Slices, not fixed arrays. The parameter type []int is a slice, which can have any length. The examples in this lesson are written as slice literals such as []int{1, 2, 3, 4, 5}.
    • The blank identifier. range always produces an index and a value for a slice. Go refuses to compile unused variables, so the index is discarded with _.
    • Early return. A return inside a loop leaves the whole function, so the remaining hurdles are skipped.
    • Empty and nil slices. Ranging over a nil slice is legal and runs the body zero times, so canJump(nil, 0) returns true.

    Complexity

    Time: O(n) for n hurdles. In the worst case, every hurdle is checked once. If an early hurdle is too tall, the function stops sooner.

    Space: O(1). Only the loop variables are used, and the slice is not copied.

    Edge cases and common mistakes

    • Equal heights. canJump([]int{3, 3}, 3) is true. Writing height >= jumperHeight in the check would wrongly reject it.
    • Empty slice. canJump([]int{}, 1) is true. A version that returns the result of the first comparison would fail here, since there is no first hurdle.
    • Returning true too early. Writing else { return true } inside the loop makes the first small hurdle decide the whole answer. []int{1, 2, 1} with a jump of 1 would give true instead of false.
    • Comparing the wrong way round. The failure test is hurdle above jump, height > jumperHeight. Reversing the operands tests the opposite condition.
    • Using the name from the examples. The function in this lesson is canJump, so call it by that name.

    Testing

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