GATE / CS ARCHIVE

SIMILAR QUESTIONS / ALGORITHMS

Verify grid dynamic-programming dependency order

2 questions solved the same way, newest first. Back to Algorithms

How these are solved

Identify north/west (and in LCS diagonal) dependencies, then test whether each proposed traversal completes predecessors before a cell. LCS allows increasing row or column order;2026 compares row-major with increasing diagonal sum.

2 marks · MCQ
Report issue

Two valid dynamic-programming orders

Dynamic Programming

Question 39, GATE 2026: T[0][k]=T[k][0]=1 for 0≤k≤n; for 1≤i,j≤n use T[i][j]=2T[i−1][j]+3T[i][j−1]. All cells initially equal 1. B1 loops i=1…n then j=1…n and updates each interior cell. B2 loops s=2…2n, then all i,j=1…n, updating only when i+j=s. Which is correct? A both; B only B1; C only B2; D neither.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Givens and goal

T[0][k]=T[k][0]=1 for 0≤k≤n; for 1≤i,j≤n use T[i][j]=2T[i−1][j]+3T[i][j−1]. All cells initially equal 1. B1 loops i=1…n then j=1…n and updates each interior cell. B2 loops s=2…2n, then all i,j=1…n, updating only when i+j=s. Which is correct? A both; B only B1; C only B2; D neither.

Approach

Check whether the dependencies are ready before each update.

Step-by-step solution

1. Each interior cell depends only on its upper neighbor (i−1,j) and left neighbor (i,j−1). The coefficients 2 and 3 do not affect the ordering requirement.
2. In B1, row i−1 has already been completed when row i begins. Within row i, j increases, so the left neighbor has already been computed. Boundary neighbors retain their initialized correct values.
3. In B2, both dependencies have coordinate sum i+j−1=s−1. They were processed in the previous diagonal layer, unless they lie on a preinitialized boundary. Every interior cell has a unique sum between 2 and 2n and is updated exactly once.
4. For a small check, T[1][1]=2+3=5; T[1][2]=2×1+3×5=17; T[2][1]=2×5+3×1=13; T[2][2]=2×17+3×13=73. Either order produces these values.
5. Both are correct, A. B1 performs Θ(n²) work, whereas the stated B2 scans n² pairs for Θ(n) values of s and takes Θ(n³). Correctness does not require equal efficiency.

Original paper, page 26 · Answer key

2 marks · MCQ
Report issue

Legal LCS dynamic-programming fill orders

Dynamic Programming

Question 54, GATE 2009: Store the LCS recurrence in L[m+1, n+1]. Which is true? A every cell must first be zero; B increasing row-major or column-major computation works; C neither works; D L[p, q] must precede L[r, s] whenever p<r OR q<s.
Question 54, GATE 2009: Store the LCS recurrence in L[m+1, n+1]. Which is true? A every cell must first be zero; B increasing row-major or column-major computation works; C neither works; D L[p, q] must precede L[r, s] whenever p<r OR q<s.
Question 54, GATE 2009: Store the LCS recurrence in L[m+1, n+1]. Which is true? A every cell must first be zero; B increasing row-major or column-major computation works; C neither works; D L[p, q] must precede L[r, s] whenever p<r OR q<s.
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Initialize only the empty-prefix boundary L[0, j]=L[i, 0]=0. Every other L[i, j] depends on L[i−1, j−1] in the matching case, or on L[i−1, j] and L[i, j−1] in the nonmatching case. Increasing-index row-major order computes all predecessors first; increasing-index column-major order also does so. Therefore B. Initializing every cell to zero is not necessary (A), and C contradicts these valid orders. D overstates the dependency: p<r alone does not require an arbitrarily far-right cell L[p, q] before L[r, s]; its OR condition can even demand opposite orders for incomparable cells such as (1, 2) and (2, 1).

The dependency arrows point left, up or diagonally up-left. Both increasing row order and increasing column order place all those predecessor cells before the current cell. Only row 0 and column 0 require initialization; every interior cell can be assigned when reached. The proposed OR ordering in D is too strong: cells(1, 2) and(2, 1) would each be required before the other. Therefore B.

Original paper, page 12