Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Zac Bolton
Buttom up DP approach has bug in dp's first-row initialization

Zac Bolton

Jun 26, 2025

The initialization for the first row in the bottom up DP approach is:

    # with only one number, we can form a subset only when the required sum is equal to 

    # that number

    for j in range(0, int(s/2)+1):

      dp[0][j] = num[0] == j

However this is wrong and could cause compounding wrong answers resulting in an incorrect output.

The issue is that it overwrites the dp[0][0] cell to False, when it should be true. The fix is to start the for loop at 1, instead of 0.

    for j in range(1, int(s/2)+1):

      dp[0][j] = num[0] == j

0

0

Comments
Comments

On this page