data-structures · intermediate · ~15 min

Unique grid paths

Count right/down paths across a grid.

Challenge

Implement:

long long unique_paths(int rows, int cols);

Return the number of distinct paths from the top-left to the bottom-right of a rows x cols grid, moving only right or down.

Input format

Grid dimensions.

Output format

Number of paths.

Constraints

Use long long; counts grow large.

Starter code

#include <stddef.h>
/* Number of paths from top-left to bottom-right of a rows x cols grid, moving only right or down. */
long long unique_paths(int rows,int cols){ (void)rows;(void)cols; return 0; }

Common mistakes

Recomputing overlapping subpaths recursively (exponential) — use a table.

Edge cases to handle

A single row or column has exactly 1 path.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.