FastPrepNumber of Ways to Reach a Position After Exactly K Steps

Number of Ways to Reach a Position After Exactly K Steps

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

You stand at integer position startPos. Each step moves exactly one unit left or right.

Return the number of distinct step sequences that reach endPos after exactly k steps, modulo 1,000,000,007.

Function

numberOfWays(startPos: int, endPos: int, k: int) → int

Examples

Example 1

startPos = 1endPos = 2k = 3return = 3

Exactly two right moves and one left move are needed, which can be ordered in three ways.

Example 2

startPos = 2endPos = 5k = 10return = 0

The distance and step count have different parity.

Constraints

  • -1000 <= startPos, endPos <= 1000.
  • 0 <= k <= 1000.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int numberOfWays(int startPos, int endPos, int k) {
    // Write your code here.
}
startPos1
endPos2
k3
expected3
Checking account…