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 code that calculates the length of the longest increasing subsequence?
DSA Python
def length_of_lis(nums): if not nums: return 0 dp = [1] * len(nums) for i in range(1, len(nums)): for j in range(i): if nums[i] > nums[j]: dp[i] = max(dp[i], dp[j] + 1) return max(dp) print(length_of_lis([10, 9, 2, 5, 3, 7, 101, 18]))
Attempts:
2 left
💡 Hint
Trace the dp array updates for each element.
✗ Incorrect
The longest increasing subsequence is [2, 3, 7, 101], which has length 4.
❓ Predict Output
intermediateOutput of LIS reconstruction
What is the printed output of this code that reconstructs one longest increasing subsequence?
DSA Python
def lis_sequence(nums): if not nums: return [] dp = [1] * len(nums) prev = [-1] * len(nums) max_len = 1 max_index = 0 for i in range(1, len(nums)): for j in range(i): if nums[i] > nums[j] and 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 lis = [] while max_index != -1: lis.append(nums[max_index]) max_index = prev[max_index] return lis[::-1] print(lis_sequence([0, 8, 4, 12, 2, 10, 6, 14, 1, 9]))
Attempts:
2 left
💡 Hint
Follow the prev array to reconstruct the LIS.
✗ Incorrect
The longest increasing subsequence found is [0, 8, 12, 14].
🔧 Debug
advancedIdentify the error in LIS length code
What error does this code raise when run?
DSA Python
def length_of_lis(nums): dp = [1] * len(nums) for i in range(len(nums)): for j in range(i): if nums[i] > nums[j]: dp[i] = max(dp[i], dp[j] + 1) return max(dp) print(length_of_lis([]))
Attempts:
2 left
💡 Hint
What happens when max() is called on an empty list?
✗ Incorrect
When nums is empty, dp is empty, so max(dp) raises ValueError because max() cannot operate on an empty sequence.
🧠 Conceptual
advancedTime complexity of LIS dynamic programming
What is the time complexity of the classic dynamic programming solution for the longest increasing subsequence problem shown below?
DSA Python
def length_of_lis(nums): dp = [1] * len(nums) for i in range(len(nums)): for j in range(i): if nums[i] > nums[j]: dp[i] = max(dp[i], dp[j] + 1) return max(dp)
Attempts:
2 left
💡 Hint
Count the nested loops and their ranges.
✗ Incorrect
The solution uses two nested loops each up to n, so time complexity is O(n^2).
🚀 Application
expertLongest Increasing Subsequence length with binary search optimization
What is the output of this optimized LIS length code using binary search?
DSA Python
import bisect def length_of_lis(nums): sub = [] for x in nums: i = bisect.bisect_left(sub, x) if i == len(sub): sub.append(x) else: sub[i] = x return len(sub) print(length_of_lis([3, 5, 7, 1, 2, 8]))
Attempts:
2 left
💡 Hint
Track how the sub list changes with each element.
✗ Incorrect
The longest increasing subsequence length is 4, for example [3,5,7,8].
