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 this Rust code that calculates the length of the Longest Increasing Subsequence (LIS) for the given array?
DSA Rust
fn length_of_lis(nums: Vec<i32>) -> usize {
let mut dp = vec![1usize; nums.len()];
for i in 1..nums.len() {
for j in 0..i {
if nums[i] > nums[j] {
dp[i] = dp[i].max(dp[j] + 1usize);
}
}
}
*dp.iter().max().unwrap_or(&0usize)
}
fn main() {
let nums = vec![10, 9, 2, 5, 3, 7, 101, 18];
println!("{}", length_of_lis(nums));
}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.
❓ Predict Output
intermediateOutput of LIS reconstruction code
What is the printed output of this Rust code that reconstructs one Longest Increasing Subsequence from the input array?
DSA Rust
fn lis_sequence(nums: Vec<i32>) -> Vec<i32> {
let n = nums.len();
let mut dp = vec![1; n];
let mut prev = vec![None; n];
for i in 1..n {
for j in 0..i {
if nums[i] > nums[j] && dp[i] < dp[j] + 1 {
dp[i] = dp[j] + 1;
prev[i] = Some(j);
}
}
}
let mut max_index = 0;
for i in 1..n {
if dp[i] >= dp[max_index] {
max_index = i;
}
}
let mut lis = Vec::new();
let mut current = Some(max_index);
while let Some(i) = current {
lis.push(nums[i]);
current = prev[i];
}
lis.reverse();
lis
}
fn main() {
let nums = vec![0, 8, 4, 12, 2, 10, 6, 14, 1, 9];
let result = lis_sequence(nums);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Look for the LIS with maximum length and reconstruct by following prev indices.
✗ Incorrect
The LIS found is [0, 4, 6, 9], which is one valid longest increasing subsequence.
🔧 Debug
advancedIdentify the error in LIS length code
This Rust code attempts to compute the length of the Longest Increasing Subsequence but produces incorrect results. What is the cause of the error?
DSA Rust
fn length_of_lis(nums: Vec<i32>) -> usize {
let mut dp = vec![0usize; nums.len()];
for i in 0..nums.len() {
dp[i] = 1usize;
for j in 0..i {
if nums[i] > nums[j] {
dp[i] = dp[i].max(dp[j] + 1usize);
}
}
}
*dp.iter().max().unwrap_or(&0usize)
}
fn main() {
let nums = vec![3, 1, 2, 1, 8];
println!("{}", length_of_lis(nums));
}Attempts:
2 left
💡 Hint
Check the condition that compares elements for increasing order.
✗ Incorrect
Using '>=' allows equal elements, which is not strictly increasing, so the LIS length is overcounted.
🧠 Conceptual
advancedTime complexity of LIS algorithms
Which of the following statements correctly describes the time complexity of common algorithms to find the length of the Longest Increasing Subsequence (LIS)?
Attempts:
2 left
💡 Hint
Consider how nested loops and binary search affect time complexity.
✗ Incorrect
The classic DP approach uses two nested loops, resulting in O(n^2). The optimized method uses binary search on a tails array, achieving O(n log n).
🚀 Application
expertLongest Increasing Subsequence in a sequence with duplicates
Given the input array [1, 3, 5, 4, 7, 4, 8], what is the length of the Longest Strictly Increasing Subsequence?
Attempts:
2 left
💡 Hint
Remember the subsequence must be strictly increasing, no equal elements allowed.
✗ Incorrect
One LIS is [1, 3, 4, 7, 8], length 5. Duplicates like the second 4 cannot be included if it breaks strict increase.
