Challenge - 5 Problems
LIS Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of LIS length calculation
What is the output of the following JavaScript code that calculates the length of the Longest Increasing Subsequence (LIS) in the array?
DSA Javascript
function lengthOfLIS(nums) {
const dp = Array(nums.length).fill(1);
for (let i = 1; i < nums.length; i++) {
for (let j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}
return Math.max(...dp);
}
console.log(lengthOfLIS([10,9,2,5,3,7,101,18]));Attempts:
2 left
💡 Hint
Think about the longest increasing subsequence in the array [10,9,2,5,3,7,101,18].
✗ Incorrect
The longest increasing subsequence is [2,3,7,101,18] but 18 is less than 101 so the actual LIS is [2,3,7,101] which has length 4. However, 18 is less than 101 so the LIS is actually [2,3,7,18] which has length 4. The correct LIS length is 4.
❓ Predict Output
intermediateOutput of LIS reconstruction code
What is the output of the following code that reconstructs one Longest Increasing Subsequence from the array?
DSA Javascript
function findLIS(nums) {
const dp = Array(nums.length).fill(1);
const prev = Array(nums.length).fill(-1);
let maxIndex = 0;
for (let i = 1; i < nums.length; i++) {
for (let j = 0; j < i; j++) {
if (nums[i] > nums[j] && dp[i] < dp[j] + 1) {
dp[i] = dp[j] + 1;
prev[i] = j;
}
}
if (dp[i] > dp[maxIndex]) maxIndex = i;
}
const lis = [];
for (let i = maxIndex; i >= 0; i = prev[i]) {
lis.push(nums[i]);
if (prev[i] === -1) break;
}
return lis.reverse();
}
console.log(findLIS([0,8,4,12,2,10,6,14,1,9]));Attempts:
2 left
💡 Hint
Trace the dp and prev arrays to find the LIS path.
✗ Incorrect
The longest increasing subsequence found by the code is [0, 8, 12, 14]. It is one valid LIS of length 4 in the array.
🧠 Conceptual
advancedTime complexity of LIS dynamic programming approach
What is the time complexity of the classic dynamic programming solution to find the length of the Longest Increasing Subsequence in an array of length n?
Attempts:
2 left
💡 Hint
Consider the nested loops in the DP approach.
✗ Incorrect
The classic DP approach uses two nested loops over the array, resulting in O(n^2) time complexity.
❓ Predict Output
advancedOutput of LIS length using binary search optimization
What is the output of the following JavaScript code that uses a binary search approach to find the length of the Longest Increasing Subsequence?
DSA Javascript
function lengthOfLIS(nums) {
const tails = [];
for (const num of nums) {
let left = 0, right = tails.length;
while (left < right) {
const mid = Math.floor((left + right) / 2);
if (tails[mid] < num) left = mid + 1;
else right = mid;
}
tails[left] = num;
}
return tails.length;
}
console.log(lengthOfLIS([3,5,7,1,2,8]));Attempts:
2 left
💡 Hint
The tails array stores the smallest possible tail for increasing subsequences of different lengths.
✗ Incorrect
The LIS length is 4 for the subsequence [3,5,7,8] or [1,2,8]. The code returns tails.length which is 4.
🔧 Debug
expertIdentify the error in LIS reconstruction code
The following code attempts to reconstruct the Longest Increasing Subsequence but produces incorrect output. What is the cause of the error?
DSA Javascript
function reconstructLIS(nums) {
const dp = Array(nums.length).fill(1);
const prev = Array(nums.length).fill(-1);
let maxLen = 1, maxIndex = 0;
for (let i = 1; i < nums.length; i++) {
for (let j = 0; j < i; j++) {
if (nums[i] > nums[j] && dp[i] < dp[j] + 1) {
dp[i] = dp[j] + 1;
prev[i] = j;
}
}
if (dp[i] > maxLen) {
maxLen = dp[i];
maxIndex = i;
}
}
const lis = [];
let current = maxIndex;
while (current !== -1) {
lis.push(nums[current]);
current = prev[current];
}
return lis;
}
console.log(reconstructLIS([10,9,2,5,3,7,101,18]));Attempts:
2 left
💡 Hint
Check how the LIS array is built and returned.
✗ Incorrect
The code builds the LIS array from the end to the start but does not reverse it before returning, so the output is reversed.
