FastPrepCount Routes Through a City Grid

Count Routes Through a City Grid

IMC Trading logoIMC Trading● EasyFULLTIMENEW GRADPHONE SCREEN
Learn

Problem statement

A city is represented by a rectangular grid with rows rows and columns columns. Start at the top-left cell and reach the bottom-right cell.

For this exercise, assume each step moves exactly one cell right or one cell down, every cell is open, and there is no wrapping. Count distinct step sequences that reach the destination. For this exercise, assume the grid dimensions are supplied as parameters and the result is an exact signed64-bit count with no modulus.

A one-cell grid has one route: the empty sequence. Complete countCityGridRoutes(rows, columns).

Function

countCityGridRoutes(rows: int, columns: int) → long

Examples

Example 1

rows = 3columns = 3return = 6

Each route has two right moves and two down moves. Choosing their order gives 6 routes. These dimensions are an authored practice example.

Example 2

rows = 1columns = 1return = 1

The start is already the destination, so the empty sequence is the single route.

Example 3

rows = 1columns = 5return = 1

Every step must go right, giving exactly one route.

Example 4

rows = 25columns = 40return = 156655690918541325

Choose positions of the24downmoves among63steps: C(63,24)=156655690918541325. This odd integer exceeds the exact-integer range of an IEEE754double, so keep the count in signed64-bit arithmetic.

Constraints

  • For this exercise, assume 1 <= rows <= 25 and 1 <= columns <= 40.
  • The largest result is 156655690918541325, which fits a signed64-bit integer.
  • Do not use floating-point rounding to count routes.

More IMC Trading problems

See IMC Trading hiring insights
public long countCityGridRoutes(int rows, int columns) {
    // Write your solution here.
}
rows3
columns3
expected6
Checking account…