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?
. . . . . # . . . . . # # . . .
- A. 20
- B. 6
- C. 3
- 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.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- DFS-based topological sort on the DAG with adjacency lists a: [b, c], b: [d], c: [d, e], d: [f], e: [f], f: [] starts at a, visits…
- DFS with white/grey/black colours detects a cycle in the directed graph with adjacency lists 1: [2, 3], 2: [4], 3: [4], 4: [5], 5: [3]…
- Keys 1 to 7 are inserted in increasing order into an empty AVL tree, then 4 is deleted (a node with two children is replaced by its…
- What is the size of a minimum vertex cover (a smallest set of vertices touching every edge) of the tree with edges 1–2, 1–3, 2–4, 2–5…
- In the tree with edges 1–2, 1–3, 2–4, 2–5, 3–6, 5–7, the rerooting technique computes S(v) = sum of distances from v to every other node…
- Each node of this binary tree holds a value; how many DOWNWARD paths (starting at any node, moving only to children, ending at any node)…