Ctrl K

Walks of Exactly k Steps

Medium
A token stands at point start on an infinite number line that includes negative numbers. Each step moves it one unit left or right. Count the ways the token can end up at point finish after exactly steps steps. Two ways differ if their sequences of steps differ. Return the answer modulo 10⁹ + 7.
Example 1
start=3, finish=1, steps=4 → 4

The walk needs three steps left and one step right, and the right step can be any of the 4: 3→4→3→2→1, 3→2→3→2→1, 3→2→1→2→1, 3→2→1→0→1.

Example 2
start=2, finish=2, steps=3 → 0

Every step flips the parity of the position, so an odd number of steps can never bring you back to where you started.

Constraints
  • -1000 ≤ start, finish ≤ 1000
  • 0 ≤ steps ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCounting Grid Routes MediumNobody Gets Their Own Gift MediumSegments on a Dotted Line MediumSorted Vowel Words MediumVowels Across All Pieces Medium
loading Python…
Input
start=3, finish=1, steps=4
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 41 tests on the server, hidden ones included.