Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the dp array with 1s for each element.
DSA Javascript
const length = nums.length;
const dp = new Array(length).fill([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Initializing dp with 0 instead of 1.
Using undefined or null which causes errors.
✗ Incorrect
Each element starts with a subsequence length of 1 (the element itself).
2fill in blank
mediumComplete the code to compare current and previous elements for increasing order.
DSA Javascript
for (let i = 1; i < length; i++) { for (let j = 0; j < i; j++) { if (nums[i] [1] nums[j]) { dp[i] = Math.max(dp[i], dp[j] + 1); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' or '<=' which allows equal elements.
Using '<' or '<=' in the wrong direction.
✗ Incorrect
We want to find strictly increasing subsequences, so nums[i] must be greater than nums[j].
3fill in blank
hardFix the error in returning the maximum length from dp array.
DSA Javascript
return Math.[1](...dp);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Math.min which gives the smallest value.
Trying to sum or average which is incorrect.
✗ Incorrect
The longest increasing subsequence length is the maximum value in dp array.
4fill in blank
hardFill both blanks to complete the nested loop for LIS calculation.
DSA Javascript
for (let [1] = 1; [1] < length; [1]++) { for (let [2] = 0; [2] < [1]; [2]++) { if (nums[[1]] > nums[[2]]) { dp[[1]] = Math.max(dp[[1]], dp[[2]] + 1); } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using same variable name for both loops.
Using variables that do not follow common conventions.
✗ Incorrect
The outer loop uses 'i' and inner loop uses 'j' to compare elements for LIS.
5fill in blank
hardFill all three blanks to create the full LIS function.
DSA Javascript
function lengthOfLIS(nums) {
const length = nums.length;
if (length === 0) return 0;
const dp = new Array(length).fill([1]);
for (let [2] = 1; [2] < length; [2]++) {
for (let [3] = 0; [3] < [2]; [3]++) {
if (nums[[2]] > nums[[3]]) {
dp[[2]] = Math.max(dp[[2]], dp[[3]] + 1);
}
}
}
return Math.max(...dp);
}Attempts:
3 left
💡 Hint
Common Mistakes
Initializing dp with 0.
Using wrong loop variable names.
Not returning the maximum value from dp.
✗ Incorrect
Initialize dp with 1, use i and j as loop variables for LIS calculation.
