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 Java code that calculates the length of the Longest Increasing Subsequence (LIS) for the given array?
DSA Java
public class LIS { public 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 max = 0; for (int length : dp) { if (length > max) max = length; } return max; } public static void main(String[] args) { int[] arr = {10, 9, 2, 5, 3, 7, 101, 18}; System.out.println(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.
🧠 Conceptual
intermediateUnderstanding LIS subsequence reconstruction
Given the dp array from LIS length calculation, which additional data structure helps reconstruct the actual longest increasing subsequence?
Attempts:
2 left
💡 Hint
Think about how to trace back the LIS elements after computing lengths.
✗ Incorrect
To reconstruct the LIS, we keep track of the previous index for each element that forms the LIS, allowing us to backtrack from the maximum dp index.
❓ Predict Output
advancedOutput of LIS length using binary search optimization
What is the output of the following Java code that uses a binary search approach to find the length of the Longest Increasing Subsequence?
DSA Java
import java.util.*; public class LISBinarySearch { public static int lengthOfLIS(int[] nums) { ArrayList<Integer> sub = new ArrayList<>(); for (int x : nums) { int i = Collections.binarySearch(sub, x); if (i < 0) i = -(i + 1); if (i == sub.size()) sub.add(x); else sub.set(i, x); } return sub.size(); } public static void main(String[] args) { int[] arr = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9}; System.out.println(lengthOfLIS(arr)); } }
Attempts:
2 left
💡 Hint
The binary search method builds a list representing the smallest possible tail for increasing subsequences of different lengths.
✗ Incorrect
The longest increasing subsequence length is 5 for the input array, for example [0, 2, 6, 9, 14].
🔧 Debug
advancedIdentify the error in LIS length calculation code
What error will the following Java code produce when run, and why?
DSA Java
public class LISBug { public static int lengthOfLIS(int[] nums) { int n = nums.length; int[] dp = new int[n]; for (int i = 1; i < n; i++) { dp[i] = 1; for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) { dp[i] = Math.max(dp[i], dp[j] + 1); } } } int max = 0; for (int length : dp) { if (length > max) max = length; } return max; } public static void main(String[] args) { int[] arr = {3, 10, 2, 1, 20}; System.out.println(lengthOfLIS(arr)); } }
Attempts:
2 left
💡 Hint
Check initialization of dp array elements before loops.
✗ Incorrect
dp[0] remains 0 because the loop starts from i=1 and dp[0] is never set to 1, so max remains 0 or less than expected.
🚀 Application
expertLongest Increasing Subsequence in a real-world scenario
You have a list of daily stock prices. You want to find the longest period where the stock price strictly increases day by day. Which algorithmic approach best fits this problem and why?
Attempts:
2 left
💡 Hint
Think about continuous days versus subsequence with gaps.
✗ Incorrect
The problem asks for the longest continuous increasing period (subarray), not subsequence. Sliding window or two-pointer approach is best for subarrays.
