Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the dp array with 1s for each element.
DSA Typescript
const dp = new Array(nums.length).fill([1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing dp with 0 instead of 1
Using nums.length as fill value
✗ Incorrect
Each element starts with a subsequence length of 1 (the element itself).
2fill in blank
mediumComplete the code to check if nums[j] is less than nums[i] to extend the subsequence.
DSA Typescript
if (nums[j] [1] nums[i]) {
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of <
Using >= which allows equal values
✗ Incorrect
We extend the subsequence only if nums[j] is smaller than nums[i].
3fill in blank
hardFix the error in updating dp[i] to the maximum length found so far.
DSA Typescript
dp[i] = Math.[1](dp[i], dp[j] + 1);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Math.min which gives the shortest length
Using Math.floor or Math.round which are unrelated
✗ Incorrect
We want the longest subsequence, so use Math.max to update dp[i].
4fill in blank
hardFill both blanks to find the maximum length in dp array after processing.
DSA Typescript
const maxLength = dp.[1]((a, b) => a [2] b ? a : b);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using max as a method (not valid on arrays)
Using < which finds minimum
✗ Incorrect
Use reduce with > to find the maximum value in dp.
5fill in blank
hardFill all three blanks to complete the LIS function that returns the length of the longest increasing subsequence.
DSA Typescript
function lengthOfLIS(nums: number[]): number {
const dp = new Array(nums.length).fill([1]);
for (let i = 1; i < nums.length; i++) {
for (let j = 0; j < i; j++) {
if (nums[j] [2] nums[i]) {
dp[i] = Math.[3](dp[i], dp[j] + 1);
}
}
}
return dp.reduce((a, b) => a > b ? a : b);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of 1 for dp initialization
Using > instead of < in condition
Using Math.min instead of Math.max
✗ Incorrect
Initialize dp with 1, check if nums[j] < nums[i], and update dp[i] with Math.max.