Data Structures & AlgorithmsHardInfosys · Accenture pattern

A robot moves only right or down from the top-left to the bottom-right cell of this 4 × 4 grid ('#' is blocked). How many paths are there?

A robot moves only right or down from the top-left to the bottom-right cell of this 4 × 4 grid ('#' is blocked). How many paths are there?
. . . .
. # . .
. . . #
# . . .
  1. A. 20
  2. B. 6
  3. C. 3
  4. D. 4

Correct answer: C. 3

dp[i][j] = dp[i−1][j] + dp[i][j−1], and 0 on '#'. Row 0: 1 1 1 1. Row 1: 1 0 1 2. Row 2: 1 1 2 0. Row 3: 0 1 3 3 – the corner gets 0 from above (blocked) + 3 from the left = 3. 20 = C(6, 3) is the count with no obstacles; forgetting that (2,3) is blocked lets paths come down the right edge and inflates the count.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic