0
0
DSA Cprogramming~20 mins

Longest Increasing Subsequence in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LIS Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output 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
#include <stdio.h>

int lengthOfLIS(int* nums, int numsSize) {
    int dp[numsSize];
    int max_len = 1;
    for (int i = 0; i < numsSize; i++) {
        dp[i] = 1;
        for (int j = 0; j < i; j++) {
            if (nums[i] > nums[j] && dp[i] < dp[j] + 1) {
                dp[i] = dp[j] + 1;
            }
        }
        if (max_len < dp[i]) {
            max_len = dp[i];
        }
    }
    return max_len;
}

int main() {
    int arr[] = {10, 9, 2, 5, 3, 7, 101, 18};
    int size = sizeof(arr) / sizeof(arr[0]);
    printf("%d\n", lengthOfLIS(arr, size));
    return 0;
}
A4
B3
C5
D6
Attempts:
2 left
💡 Hint
Trace the dp array updates for each element to find the longest increasing subsequence length.
Predict Output
intermediate
2:00remaining
Output of LIS length with repeated elements
What is the output of the following C code that calculates the length of the Longest Increasing Subsequence (LIS) for the given array with repeated elements?
DSA C
#include <stdio.h>

int lengthOfLIS(int* nums, int numsSize) {
    int dp[numsSize];
    int max_len = 1;
    for (int i = 0; i < numsSize; i++) {
        dp[i] = 1;
        for (int j = 0; j < i; j++) {
            if (nums[i] > nums[j] && dp[i] < dp[j] + 1) {
                dp[i] = dp[j] + 1;
            }
        }
        if (max_len < dp[i]) {
            max_len = dp[i];
        }
    }
    return max_len;
}

int main() {
    int arr[] = {3, 4, 2, 8, 10, 5, 1};
    int size = sizeof(arr) / sizeof(arr[0]);
    printf("%d\n", lengthOfLIS(arr, size));
    return 0;
}
A4
B5
C3
D6
Attempts:
2 left
💡 Hint
Check subsequences like {3,4,8,10} and {2,5} to find the longest increasing subsequence.
Predict Output
advanced
2:00remaining
Output 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 binary search optimization?
DSA C
#include <stdio.h>

int binarySearch(int* tail, int left, int right, int key) {
    while (right - left > 1) {
        int mid = left + (right - left) / 2;
        if (tail[mid] >= key)
            right = mid;
        else
            left = mid;
    }
    return right;
}

int lengthOfLIS(int* nums, int numsSize) {
    if (numsSize == 0) return 0;
    int tail[numsSize];
    int length = 1;
    tail[0] = nums[0];
    for (int i = 1; i < numsSize; i++) {
        if (nums[i] < tail[0])
            tail[0] = nums[i];
        else if (nums[i] > tail[length - 1])
            tail[length++] = nums[i];
        else {
            int pos = binarySearch(tail, -1, length - 1, nums[i]);
            tail[pos] = nums[i];
        }
    }
    return length;
}

int main() {
    int arr[] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9};
    int size = sizeof(arr) / sizeof(arr[0]);
    printf("%d\n", lengthOfLIS(arr, size));
    return 0;
}
A5
B4
C6
D7
Attempts:
2 left
💡 Hint
The binary search helps maintain the smallest possible tail values for subsequences of different lengths.
🧠 Conceptual
advanced
1:30remaining
Understanding LIS subsequence reconstruction
Which data structure is most suitable to reconstruct the actual Longest Increasing Subsequence after computing its length using dynamic programming?
APriority queue to get maximum elements quickly
BHash map to store element counts
CStack to store indices of elements in reverse order
DQueue to store elements in order of appearance
Attempts:
2 left
💡 Hint
Think about how to retrieve the subsequence in correct order after tracing back from the end.
🔧 Debug
expert
2:30remaining
Identify the bug in LIS length calculation code
What error does the following C code produce when run, and why?
DSA C
#include <stdio.h>

int lengthOfLIS(int* nums, int numsSize) {
    int dp[numsSize];
    int max_len = 1;
    for (int i = 0; i <= numsSize; i++) {
        dp[i] = 1;
        for (int j = 0; j < i; j++) {
            if (nums[i] > nums[j] && dp[i] < dp[j] + 1) {
                dp[i] = dp[j] + 1;
            }
        }
        if (max_len < dp[i]) {
            max_len = dp[i];
        }
    }
    return max_len;
}

int main() {
    int arr[] = {1, 3, 2, 4};
    int size = sizeof(arr) / sizeof(arr[0]);
    printf("%d\n", lengthOfLIS(arr, size));
    return 0;
}
ARuns correctly and returns 4
BReturns incorrect LIS length 3 instead of 4
CCompilation error due to missing return statement
DSegmentation fault due to out-of-bounds array access
Attempts:
2 left
💡 Hint
Check the loop boundary conditions carefully for array indexing.