IAE Academy

GATE 2026 CS2 Q.39: solution and answer

GATE 2026 · CS2Q.39 · 2 marks · MCQ

Consider a table , where the elements , , represent the cost of the optimal solutions of different subproblems of a problem that is being solved using a dynamic programming algorithm. The recursive formulation to compute the table entries is as follows:


Consider the following two algorithms to compute entries of . Assume that for both the algorithms, for all , has been initialized to 1.

Algorithm :

For i = 1, 2, ..., n
    For j = 1, 2, ..., n
        T[i][j] = 2T[i-1][j] + 3T[i][j-1]

Algorithm :

For s = 2, 3, ..., 2n
    For i = 1, 2, ..., n
        For j = 1, 2, ..., n
            If (i + j == s)
                T[i][j] = 2T[i-1][j] + 3T[i][j-1]

Algorithm , is said to be correct if and only if it calculates the correct values of , for all , (as per the recursive formulation) at the end of the execution of the algorithm .

Which one of the following statements is true?

  1. (A)

    Both algorithms and are correct

  2. (B)

    Algorithm is correct, but algorithm is incorrect

  3. (C)

    Algorithm is correct, but algorithm is incorrect

  4. (D)

    Both algorithms and are incorrect

See the question in the paper

Answer and solution

ANSWER

The correct answer is (A): both algorithms and correctly compute every entry of the table .

Official answer: ASource: Official answer key, GATE 2026 CS2 (IIT Guwahati)

  1. The recurrence for interior cells is for , and every cell with or is a boundary value fixed at by . Neither algorithm's loop body ever touches a boundary cell (both inner loops range over from to ), so the boundary values stay correct throughout.

  2. Algorithm (row-major: outer, inner). Fix a row . By induction on : is the correct boundary value and is never overwritten before is computed. When is computed, was computed earlier in the same row's -loop (since ) and is therefore already correct by the induction hypothesis, and was computed and finalized during the previous row's iteration (row is fully completed before row starts) and is also correct. So every is computed from two already-correct values. By induction over (rows) and (columns within a row), computes every entry correctly.

  3. Algorithm (anti-diagonal: outer loop , inner loops scan all and update those with ). A cell with depends only on , whose index sum is , and , whose index sum is also . Every cell with index sum is updated during a strictly earlier outer iteration () and is therefore already correct by induction on (base case : dependencies have sum , i.e. or , both untouched boundary values equal to ).

  4. Within one fixed , the cells satisfying (e.g., for : ) never depend on each other — each one's two dependencies always lie on diagonal , never on diagonal itself. So the order in which are scanned inside a fixed is irrelevant to correctness; every cell on diagonal is computed only from already-finalized diagonal- (or boundary) values. By induction over , computes every entry correctly.

  5. Both 's row-major order and 's anti-diagonal order are valid topological orders of the dependency DAG in which depends only on and (each strictly decreasing by 1). Neither algorithm ever reads a cell before the cell it depends on has been finalized, so both algorithms are correct.

  6. This matches option (A): both algorithms and are correct.

KEY IDEA

Whenever a DP recurrence's dependencies always come from strictly "smaller" cells in some measure (here, strictly decreases by 1 at each dependency), any loop order that is a valid topological order of that dependency structure computes the table correctly — row-major and anti-diagonal ("wavefront") order are both such valid orders here, since each cell's two dependencies fall on the immediately preceding row/column or the immediately preceding diagonal, never on the same diagonal.

WATCH OUT FOR

Assuming that scanning order only matters when it visits cells "out of the usual row/column order," and therefore either (a) wrongly flagging as incorrect on the intuition that its triple-nested loop with an if condition looks unusual or wasteful, without checking that its dependencies are actually satisfied, or (b) failing to verify that cells sharing the same diagonal () never depend on each other, which is the fact that makes the inner scan order inside a fixed harmless.

From the GATE 2026 CS2 paper. Every question of this paper · Download the full paper · All 8 Design techniques questions, with notes

More Design techniques questions