Challenge - 5 Problems
LIS Mastery Badge
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 C# code that calculates the length of the Longest Increasing Subsequence (LIS) for the given array?
DSA C#
using System; class Program { static int LengthOfLIS(int[] nums) { int n = nums.Length; int[] dp = new int[n]; for (int i = 0; i < n; i++) dp[i] = 1; for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) { dp[i] = Math.Max(dp[i], dp[j] + 1); } } } int maxLen = 0; for (int i = 0; i < n; i++) { if (dp[i] > maxLen) maxLen = dp[i]; } return maxLen; } static void Main() { int[] arr = {10, 9, 2, 5, 3, 7, 101, 18}; Console.WriteLine(LengthOfLIS(arr)); } }
Attempts:
2 left
💡 Hint
Trace the dp array updates for each element comparing with previous elements.
✗ Incorrect
The longest increasing subsequence is [2, 3, 7, 101] which has length 4, but the code also counts [2, 3, 7, 18] as length 4. The maximum length found is 4, but the code returns 4. The correct output is 4.
❓ Predict Output
intermediateOutput of LIS sequence reconstruction
What is the output of the following C# code that reconstructs one Longest Increasing Subsequence (LIS) from the given array?
DSA C#
using System; using System.Collections.Generic; class Program { static List<int> GetLIS(int[] nums) { int n = nums.Length; int[] dp = new int[n]; int[] prev = new int[n]; for (int i = 0; i < n; i++) { dp[i] = 1; prev[i] = -1; } int maxLen = 1, maxIndex = 0; for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { if (nums[i] > nums[j] && dp[j] + 1 > dp[i]) { dp[i] = dp[j] + 1; prev[i] = j; } } if (dp[i] >= maxLen) { maxLen = dp[i]; maxIndex = i; } } List<int> lis = new List<int>(); for (int i = maxIndex; i >= 0; i = prev[i]) { lis.Add(nums[i]); if (prev[i] == -1) break; } lis.Reverse(); return lis; } static void Main() { int[] arr = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9}; var lis = GetLIS(arr); Console.WriteLine(string.Join(", ", lis)); } }
Attempts:
2 left
💡 Hint
Follow the prev array to reconstruct the LIS from the maxIndex backwards.
✗ Incorrect
The LIS found is [0, 4, 6, 9] which is one valid longest increasing subsequence for the input array.
🔧 Debug
advancedIdentify the error in LIS length calculation
What error does the following C# code produce when trying to calculate the length of the Longest Increasing Subsequence (LIS)?
DSA C#
using System; class Program { static int LengthOfLIS(int[] nums) { int n = nums.Length; int[] dp = new int[n]; for (int i = 0; i < n; i++) dp[i] = 1; for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) { dp[i] = Math.Max(dp[i], dp[j] + 1); } } } int maxLen = 0; for (int i = 0; i < n; i++) { if (dp[i] > maxLen) maxLen = dp[i]; } return maxLen; } static void Main() { int[] arr = {3, 10, 2, 1, 20}; Console.WriteLine(LengthOfLIS(arr)); } }
Attempts:
2 left
💡 Hint
Check the loop boundary conditions for i in the nested loops.
✗ Incorrect
The outer loop uses 'i <= n' which causes i to go out of bounds when accessing nums[i] and dp[i], causing IndexOutOfRangeException.
🧠 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 (LIS) using a dp array and nested loops?
Attempts:
2 left
💡 Hint
Consider the nested loops iterating over the array elements.
✗ Incorrect
The classic DP solution uses two nested loops each iterating up to n, resulting in O(n^2) time complexity.
🚀 Application
expertOutput of LIS length using binary search optimization
What is the output of the following C# code that calculates the length of the Longest Increasing Subsequence (LIS) using a binary search optimized approach?
DSA C#
using System; using System.Collections.Generic; class Program { static int LengthOfLIS(int[] nums) { List<int> sub = new List<int>(); foreach (int x in nums) { int left = 0, right = sub.Count; while (left < right) { int mid = left + (right - left) / 2; if (sub[mid] < x) left = mid + 1; else right = mid; } if (right == sub.Count) sub.Add(x); else sub[right] = x; } return sub.Count; } static void Main() { int[] arr = {3, 5, 7, 1, 2, 8}; Console.WriteLine(LengthOfLIS(arr)); } }
Attempts:
2 left
💡 Hint
The binary search approach builds a list representing 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, 7, 8]. The code returns 4.
