0
0
DSA Typescriptprogramming~20 mins

Longest Increasing Subsequence in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LIS Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of LIS length calculation
What is the output of the following TypeScript code that calculates the length of the Longest Increasing Subsequence (LIS) for the given array?
DSA Typescript
function lengthOfLIS(nums: number[]): number {
  const dp: number[] = 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]));
A4
B3
C6
D5
Attempts:
2 left
💡 Hint
Trace the dp array updates for each element comparing with previous elements.
🧠 Conceptual
intermediate
1:30remaining
Understanding LIS subsequence reconstruction
Which data structure is commonly used to reconstruct the actual Longest Increasing Subsequence after computing its length using dynamic programming?
ASet to keep unique elements of the subsequence
BQueue to store all subsequences during computation
CHash map to store frequency of elements
DStack to trace back indices from dp array
Attempts:
2 left
💡 Hint
Think about how to backtrack from the dp array to find the sequence.
🔧 Debug
advanced
2:00remaining
Identify the error in LIS length code
What error will the following TypeScript code produce when trying to compute the LIS length?
DSA Typescript
function lengthOfLIS(nums: number[]): number {
  const dp: number[] = 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([3, 4, 2, 8, 10]));
ARuntime error: Index out of bounds
BReturns incorrect LIS length 1
CSyntax error due to missing semicolon
DReturns correct LIS length 4
Attempts:
2 left
💡 Hint
Check the loop boundary conditions carefully.
🚀 Application
advanced
2:30remaining
Find LIS length with binary search optimization
What is the output of the following TypeScript code that uses binary search to find the length of the Longest Increasing Subsequence?
DSA Typescript
function lengthOfLIS(nums: number[]): number {
  const tails: number[] = [];
  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;
    }
    if (right < tails.length) tails[right] = num;
    else tails.push(num);
  }
  return tails.length;
}

console.log(lengthOfLIS([0, 8, 4, 12, 2, 10, 6, 14, 1, 9]));
A7
B5
C6
D4
Attempts:
2 left
💡 Hint
The tails array stores the smallest possible tail for increasing subsequences of different lengths.
🧠 Conceptual
expert
1:30remaining
Time complexity of LIS algorithms
Which of the following statements correctly describes the time complexity of the classic dynamic programming LIS algorithm and the binary search optimized LIS algorithm?
AClassic DP: O(n log n), Binary Search optimized: O(n^2)
BBoth algorithms have O(n^2) time complexity
CClassic DP: O(n^2), Binary Search optimized: O(n log n)
DBoth algorithms have O(n log n) time complexity
Attempts:
2 left
💡 Hint
Consider nested loops vs binary search usage.