Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the length of the longest increasing subsequence to 1 for each element.
DSA Go
for i := range nums { dp[i] = [1] }
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing dp[i] to 0 causes incorrect subsequence length.
Using nums[i] instead of 1 confuses value with length.
✗ Incorrect
Each element alone is a subsequence of length 1, so dp[i] should start at 1.
2fill in blank
mediumComplete the code to check if the current number can extend the subsequence ending at j.
DSA Go
if nums[i] [1] nums[j] { if dp[j]+1 > dp[i] { dp[i] = dp[j] + 1 } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' reverses the logic.
Using '==' does not allow increasing sequences.
✗ Incorrect
We want to extend the subsequence only if nums[i] is greater than nums[j].
3fill in blank
hardFix the error in updating the maximum length of LIS after processing each element.
DSA Go
if dp[i] [1] maxLen { maxLen = dp[i] }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes maxLen to never update correctly.
Using '<=' or '>=' can cause unnecessary updates but still work.
✗ Incorrect
We update maxLen only if dp[i] is greater than current maxLen.
4fill in blank
hardFill both blanks to complete the nested loops for LIS calculation.
DSA Go
for [1] := 1; [2] < len(nums); [2]++ { for j := 0; j < [2]; j++ { if nums[[1]] > nums[j] { if dp[j]+1 > dp[[1]] { dp[[1]] = dp[j] + 1 } } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable for both loops causes errors.
Using j for outer loop conflicts with inner loop.
✗ Incorrect
The outer loop variable is i, iterating from 1 to len(nums)-1.
5fill in blank
hardFill all three blanks to complete the LIS function returning the maximum length.
DSA Go
func lengthOfLIS(nums []int) int {
dp := make([]int, len(nums))
for i := range nums {
dp[i] = [1]
}
maxLen := [2]
for i := 1; i < len(nums); i++ {
for j := 0; j < i; j++ {
if nums[i] > nums[j] {
if dp[j]+1 > dp[i] {
dp[i] = dp[j] + 1
}
}
}
if dp[i] [3] maxLen {
maxLen = dp[i]
}
}
return maxLen
}Attempts:
3 left
💡 Hint
Common Mistakes
Initializing dp or maxLen to 0 causes wrong results.
Using '<' instead of '>' in maxLen update fails to track longest subsequence.
✗ Incorrect
Initialize dp[i] to 1, maxLen to 1, and update maxLen if dp[i] > maxLen.
