Skip to main content
Back to Samples
HardCompleted

Longest Increasing Subsequence

Dynamic programming optimization problem

C
Overall Grade
Performance Overview
Time Taken
45 minutes
Complexity Accuracy
Poor
Edge Cases Discussed
No
Alternative Solutions
No
Your Solution
function lengthOfLIS(nums) {
    let maxLength = 1;
    
    for (let i = 0; i < nums.length; i++) {
        let currentLength = 1;
        let lastNum = nums[i];
        
        for (let j = i + 1; j < nums.length; j++) {
            if (nums[j] > lastNum) {
                currentLength++;
                lastNum = nums[j];
            }
        }
        
        maxLength = Math.max(maxLength, currentLength);
    }
    
    return maxLength;
}
Incorrect algorithm - greedy approach
O(n²) time complexity, not optimal
Misses optimal substructure
AI Feedback
Critical Issues
  • • Used greedy approach instead of dynamic programming
  • • Algorithm produces incorrect results for many inputs
  • • Missed the optimal substructure property
  • • No consideration of overlapping subproblems
What You Missed

This is a classic DP problem. The key insight is that for each position, you need to consider all previous elements that are smaller and build upon their LIS lengths.

Study Recommendations
  • • Review dynamic programming fundamentals
  • • Practice identifying optimal substructure
  • • Study the O(n log n) binary search solution
  • • Work through more DP problems step by step
Correct DP Solution
function lengthOfLIS(nums) {
    if (nums.length === 0) return 0;
    
    const dp = new Array(nums.length).fill(1);
    
    for (let i = 1; i < nums.length; i++) {
        for (let j = 0; j < i; j++) {
            if (nums[j] < nums[i]) {
                dp[i] = Math.max(dp[i], dp[j] + 1);
            }
        }
    }
    
    return Math.max(...dp);
}

Key insight: dp[i] represents the length of the longest increasing subsequence ending at index i. For each position, we check all previous positions and extend their LIS if the current element is larger.

Detailed Performance Metrics
Problem Understanding45%
Code Quality35%
Communication55%
Actions