← Meta (Facebook) bank

Meta (Facebook) · Question 70

Unique Paths

Pythoncode⏱ O(m*n)💾 O(n)
Product-based

Problem

You are given m x n grid. Find number of unique paths from top-left to bottom-right.

Approach

Grid DP: each cell sums paths from top and left; one row suffices.

Time O(m*n)Space O(n)

How it works

  1. 1First row and column each have one path.
  2. 2Every other cell sums top plus left.

Sample input / output

Input:
3 7
Output:
28

Constraints

1 <= m, n <= 100; answer fits 32-bit.

Solution — read it, then walk it

Time O(m*n)Space O(n)Loops 2Branches 1Returns 1source · Python
Scroll sideways for wide charts — the view follows the active step.
↻ repeat↻ repeatyesnoSTARTm, ndp = [1] * nfor _ in range(1, m)for j in range(1, n)dp[j] += dp[j - 1]RETURN dp[n - 1]DEFINE main()import sysdata =sys.stdin.read().split()it = iter(data)m = int(next(it)); n =int(next(it))print(uniquePaths(m, n))if __name__ =="__main__"main()END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/15

StartSTART

Interactive control-flow diagram and step-by-step code walkthrough.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits