Start learning free

LESSON 141 OF 414 · LOOPS & CONDITIONALS

Hurdle Jump in Rust: Scanning for a Hurdle That Is Too Tall

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:

Input: hurdle heights = [1, 2, 3, 4, 5], jump height = 5
Output: true

Input: hurdle heights = [5, 5, 3, 4, 5], jump height = 3
Output: false

Input: hurdle heights = [5, 4, 5, 6], jump height = 10
Output: true

Input: hurdle heights = [1, 2, 1], jump height = 1
Output: false

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

STEP 1 OF 3
    Ctrl + Enter to check

    RUST · LOOPS & CONDITIONALS

    Hurdle Jump in Rust, explained

    Problem overview

    The function can_jump receives a vector of hurdle heights, hurdle_heights, and the jumper's jump height, jumper_height. It returns true if the jumper can clear every hurdle and false otherwise. A hurdle is cleared when the jump height is greater than or equal to the hurdle's height, so a jump of 5 clears a hurdle of height 5.

    Examples: heights [1, 2, 3, 4, 5] with a jump of 5 give true. Heights [5, 5, 3, 4, 5] with a jump of 3 give false, because the first hurdle is 5. Heights [1, 2, 1] with a jump of 1 give false, because the hurdle of height 2 is too tall. An empty vector gives true: with no hurdles, there is nothing to fail.

    Approach: look for one failure

    "Clears all hurdles" fails as soon as a single hurdle is too tall, so it is easier to search for a counterexample than to confirm each hurdle. Walk through the heights. If a hurdle is taller than the jump, the answer is false immediately. If the walk finishes without finding such a hurdle, every one was clearable and the answer is true.

    This also explains the empty case. With no hurdles the loop doesn't run, no counterexample exists, and the function returns true, exactly as the task wants, with no special code.

    Step by step

    1. Loop over &hurdle_heights with the variable h. The & borrows the vector, and each h is a reference to a height.
    2. Test for a hurdle that is too tall with *h > jumper_height. The comparison is strict: a hurdle exactly as tall as the jump can still be cleared. The * reads the number behind the reference.
    3. Return false inside that branch. The first hurdle that is too tall settles the answer, and the rest need no checking.
    4. Return true after the loop. It runs only if no hurdle was too tall.

    The solution in Rust

    impl Solution {
        fn can_jump(hurdle_heights: Vec<i32>, jumper_height: i32) -> bool {
            for h in &hurdle_heights {
                if *h > jumper_height {
                    return false;
                }
            }
            return true;
        }
    }
    

    The Rust details behind it:

    • The reference and the dereference. Looping over &hurdle_heights gives &i32 values. Comparing a reference with an i32 does not compile, so the test is *h > jumper_height.
    • Strict versus non-strict. The failure test is >, which is the opposite of the success rule "jump height greater than or equal to the hurdle". Using >= here would reject hurdles that are exactly as tall as the jump, so [1, 2, 3, 4, 5] with a jump of 5 would wrongly give false.
    • Return from inside a loop. return ends the function, not just the loop. After it, the remaining hurdles are never examined.
    • The final true. The function must produce a bool on every path, and the loop itself evaluates to (), so the closing return true; is required.

    Complexity

    Time: O(n) for n hurdles. In the worst case, every hurdle is checked once. A tall hurdle near the front lets the function stop early.

    Space: O(1), because only a reference to the current height is stored.

    Edge cases and common mistakes

    • Empty vector. The loop is skipped and the function returns true.
    • Equal heights. A hurdle with the same height as the jump is cleared. [5, 4, 5, 6] with a jump of 6 gives true.
    • Returning true inside the loop. A hurdle that can be cleared proves nothing about the next one. Returning true there would answer after the first small hurdle.
    • Swapping the arguments in the comparison. The test compares a hurdle with the jump height, in that order. Reversing them reverses the meaning.
    • Only checking the last or first hurdle. The tall hurdle can be anywhere, as in [1, 9, 1] with a jump of 5, which gives false.

    Testing

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