In this lesson, Alvin explores the strategy to solving the following interview problem:
Write a function, array_stepper, that takes in a list of numbers as an argument. You start at the first position of the list. The function should return a boolean indicating whether or not it is possible to reach the last position of the list. When situated at some position of the list, you may take a maximum number of steps based on the number at that position.
For example, given:
- idx = 0 1 2 3 4 5
- numbers = [2, 4, 2, 0, 0, 1]
The answer is True.
We start at idx 0, we could take 1 step or 2 steps forward.
The correct choice is to take 1 step to idx 1.
Then take 4 steps forward to the end at idx 5.
array_stepper([2, 4, 2, 0, 0, 1]) # -> True
array_stepper([2, 3, 2, 0, 0, 1]) # -> False
array_stepper([3, 1, 3, 1, 0, 1]) # -> True
array_stepper([4, 1, 5, 1, 1, 1, 0, 4]) # -> True
array_stepper([4, 1, 2, 1, 1, 1, 0, 4]) # -> False
If you need additional support taking these DSA skills and actually applying them, take Alvin's complete data structures and algorithms course on Structy. You can try out the concepts yourself in their interactive code editor and learn advanced DSA patterns like stack exhaustive recursion.
Use this link to get 20% off the entire Structy DSA learning experience.
Follow Alvin on LinkedIn: https://www.linkedin.com/in/alvin-zablan-b73a92117/