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)?
DSA C++
#include <iostream> #include <vector> #include <algorithm> int lengthOfLIS(std::vector<int>& nums) { if (nums.empty()) return 0; std::vector<int> dp(nums.size(), 1); for (size_t i = 1; i < nums.size(); ++i) { for (size_t j = 0; j < i; ++j) { if (nums[i] > nums[j]) { dp[i] = std::max(dp[i], dp[j] + 1); } } } return *std::max_element(dp.begin(), dp.end()); } int main() { std::vector<int> nums = {10, 9, 2, 5, 3, 7, 101, 18}; std::cout << lengthOfLIS(nums) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Trace the dp array updates for each element to find the longest increasing subsequence length.
✗ Incorrect
The longest increasing subsequence is {2, 3, 7, 101}, which has length 4.
🧠 Conceptual
intermediateUnderstanding LIS with binary search optimization
Which statement correctly describes the role of the 'tails' array in the binary search approach to find the length of the Longest Increasing Subsequence?
Attempts:
2 left
💡 Hint
Think about how 'tails' helps to maintain potential ends of subsequences.
✗ Incorrect
'tails[i]' is the smallest tail of all increasing subsequences with length i+1, allowing efficient updates and binary search.
🔧 Debug
advancedIdentify the runtime error in LIS code
What runtime error will the following code produce when executed?
DSA C++
#include <iostream> #include <vector> int lengthOfLIS(std::vector<int>& nums) { std::vector<int> dp(nums.size(), 1); for (int i = 1; i <= nums.size(); ++i) { for (int j = 0; j < i; ++j) { if (nums[i] > nums[j]) { dp[i] = std::max(dp[i], dp[j] + 1); } } } int max_len = 0; for (int val : dp) { if (val > max_len) max_len = val; } return max_len; } int main() { std::vector<int> nums = {3, 10, 2, 1, 20}; std::cout << lengthOfLIS(nums) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check the loop conditions and array indexing carefully.
✗ Incorrect
The loop uses i <= nums.size(), which causes i to go out of bounds when accessing nums[i] and dp[i].
📝 Syntax
advancedSyntax error in LIS code snippet
Which option correctly fixes the syntax error in this LIS code snippet?
DSA C++
std::vector<int> dp(nums.size(), 1) for (int i = 1; i < nums.size(); i++) { for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) { dp[i] = std::max(dp[i], dp[j] + 1); } } }
Attempts:
2 left
💡 Hint
Check the end of each statement for missing punctuation.
✗ Incorrect
The vector initialization line is missing a semicolon, causing a syntax error.
🚀 Application
expertLongest Increasing Subsequence reconstruction
Given the following code that computes the length of LIS, what is the printed output representing the actual LIS sequence?
DSA C++
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> nums = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9}; std::vector<int> dp(nums.size(), 1); std::vector<int> prev(nums.size(), -1); int max_len = 1, max_index = 0; for (size_t i = 1; i < nums.size(); ++i) { for (size_t 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] > max_len) { max_len = dp[i]; max_index = i; } } std::vector<int> lis_sequence; for (int i = max_index; i >= 0; i = prev[i]) { lis_sequence.push_back(nums[i]); if (prev[i] == -1) break; } std::reverse(lis_sequence.begin(), lis_sequence.end()); for (int num : lis_sequence) { std::cout << num << " "; } std::cout << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Trace the prev array to reconstruct the LIS from the maximum dp index.
✗ Incorrect
The LIS is {0, 4, 6, 9} with length 4, reconstructed by following prev indices from max_index.
