📖 Scenario: You are analyzing a sequence of daily stock prices. You want to find the longest period during which the stock price keeps increasing day by day.
🎯 Goal: Build a program that finds the length of the longest increasing subsequence in a list of stock prices.
📋 What You'll Learn
Create an array called prices with the exact values [10, 22, 9, 33, 21, 50, 41, 60].
Create a variable called n that stores the length of the prices array.
Create an array called lis of length n and initialize all elements to 1.
Use nested for loops with variables i and j to update lis based on the longest increasing subsequence logic.
Create a variable called maxLIS to store the maximum value in lis.
Print the value of maxLIS.
💡 Why This Matters
🌍 Real World
Finding the longest increasing subsequence helps in analyzing trends like stock prices, sales growth, or any data that changes over time.
💼 Career
Understanding LIS is useful in software development roles that involve algorithm design, data analysis, and optimization problems.
Progress0 / 4 steps
1
Create the stock prices array
Create an array called prices with these exact values: [10, 22, 9, 33, 21, 50, 41, 60].
DSA Javascript
Hint
Use const prices = [...] to create the array with the exact numbers.
2
Initialize length and LIS array
Create a variable called n that stores the length of the prices array. Then create an array called lis of length n and initialize all elements to 1.
DSA Javascript
Hint
Use prices.length to get the length. Use new Array(n).fill(1) to create the LIS array.
3
Calculate the LIS values
Use nested for loops with variables i and j to update the lis array. For each i from 1 to n - 1, check all j from 0 to i - 1. If prices[i] is greater than prices[j] and lis[i] is less than lis[j] + 1, then update lis[i] to lis[j] + 1.
DSA Javascript
Hint
Use two loops: outer loop i from 1 to n-1, inner loop j from 0 to i-1. Update lis[i] if conditions are met.
4
Find and print the longest increasing subsequence length
Create a variable called maxLIS that stores the maximum value in the lis array. Then print the value of maxLIS.
DSA Javascript
Hint
Use Math.max(...lis) to find the maximum value. Use console.log(maxLIS) to print it.
Practice
(1/5)
1. What does the Longest Increasing Subsequence (LIS) problem ask you to find in a sequence of numbers?
easy
A. The longest subsequence where numbers are all equal
B. The longest subsequence where each number is smaller than the previous one
C. The longest subsequence with alternating increasing and decreasing numbers
D. The longest subsequence where each number is larger than the previous one
Solution
Step 1: Understand the definition of LIS and compare options
The Longest Increasing Subsequence is a subsequence where every next number is strictly greater than the previous one. The longest subsequence where each number is larger than the previous one matches the definition exactly, others describe different patterns.
Final Answer:
The longest subsequence where each number is larger than the previous one -> Option D
Quick Check:
LIS = strictly increasing subsequence [OK]
Hint: LIS means strictly increasing numbers in order [OK]
Common Mistakes:
Confusing increasing with decreasing subsequence
Thinking LIS means contiguous elements only
Assuming equal numbers are allowed in LIS
2. Which of the following JavaScript code snippets correctly initializes an array to store LIS lengths for each element in the input array arr?
easy
A. const lis = Array(arr.length).fill(0);
B. const lis = new Array(arr.length).fill(null);
C. const lis = new Array(arr.length).fill(1);
D. const lis = new Array(arr.length).fill(undefined);
Solution
Step 1: Understand LIS initialization and check syntax
Each element starts with LIS length 1 because each element alone is a subsequence. const lis = new Array(arr.length).fill(1); correctly creates an array of length arr.length filled with 1s.
Final Answer:
const lis = new Array(arr.length).fill(1); -> Option C
Quick Check:
Initialize LIS array with 1s [OK]
Hint: LIS array starts with 1 for each element [OK]
Common Mistakes:
Filling with 0 instead of 1
Using fill(null) or fill(undefined) which are incorrect for LIS
Not matching array length to input array
3. What is the length of the Longest Increasing Subsequence for the array [10, 22, 9, 33, 21, 50, 41, 60]?
medium
A. 4
B. 5
C. 6
D. 7
Solution
Step 1: Identify increasing subsequences and confirm no longer exists
Possible LIS includes [10, 22, 33, 50, 60] which has length 5. Checking other subsequences shows none longer than 5.
4. Consider this JavaScript code snippet to find LIS length:
function lengthOfLIS(arr) {
const lis = new Array(arr.length).fill(1);
for (let i = 1; i < arr.length; i++) {
for (let j = 0; j < i; j++) {
if (arr[i] > arr[j]) {
lis[i] = Math.max(lis[i], lis[j] + 1);
}
}
}
return lis[arr.length];
}
What is the error in this code?
medium
A. Returns lis[arr.length] which is undefined, should return max value in lis array
B. Incorrect initialization of lis array with 1s
C. The inner loop should start from i instead of 0
D. The comparison should be arr[i] < arr[j] instead of arr[i] > arr[j]
Solution
Step 1: Check return statement and correct return logic
Returning lis[arr.length] accesses out-of-bounds index (undefined). Should return the maximum value in lis array, e.g., Math.max(...lis).
Final Answer:
Returns lis[arr.length] which is undefined, should return max value in lis array -> Option A
Quick Check:
Return max LIS length, not out-of-bounds element [OK]
Hint: Return max value in lis array, not lis[arr.length] [OK]
Common Mistakes:
Returning wrong index from lis array
Misunderstanding loop ranges
Reversing comparison operators
5. Given the array [3, 10, 2, 1, 20], which of the following code snippets correctly finds the length of the Longest Increasing Subsequence using binary search optimization?
hard
A. function lengthOfLIS(arr) {
const tails = [];
for (const num of arr) {
let left = 0, right = tails.length;
while (left < right) {
const mid = Math.floor((left + right) / 2);
if (tails[mid] < num) left = mid + 1;
else right = mid;
}
tails[left] = num;
}
return tails.length;
}
B. function lengthOfLIS(arr) {
const tails = [];
for (const num of arr) {
let left = 0, right = tails.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (tails[mid] <= num) left = mid + 1;
else right = mid - 1;
}
tails[left] = num;
}
return tails.length;
}
C. function lengthOfLIS(arr) {
const tails = [];
for (const num of arr) {
let left = 0, right = tails.length;
while (left < right) {
const mid = Math.ceil((left + right) / 2);
if (tails[mid] > num) right = mid - 1;
else left = mid + 1;
}
tails[left] = num;
}
return tails.length;
}
D. function lengthOfLIS(arr) {
const tails = [];
for (const num of arr) {
let left = 0, right = tails.length;
while (left < right) {
const mid = Math.floor((left + right) / 2);
if (tails[mid] > num) right = mid;
else left = mid + 1;
}
tails[left] = num;
}
return tails.length;
}
Solution
Step 1: Understand binary search in tails array and check boundaries/conditions
We find the position to replace or append the current number to keep tails sorted. function lengthOfLIS(arr) {
const tails = [];
for (const num of arr) {
let left = 0, right = tails.length;
while (left < right) {
const mid = Math.floor((left + right) / 2);
if (tails[mid] < num) left = mid + 1;
else right = mid;
}
tails[left] = num;
}
return tails.length;
} uses correct boundaries and conditions: left < right, mid floor, tails[mid] < num moves left up.
Final Answer:
function lengthOfLIS(arr) {
const tails = [];
for (const num of arr) {
let left = 0, right = tails.length;
while (left < right) {
const mid = Math.floor((left + right) / 2);
if (tails[mid] < num) left = mid + 1;
else right = mid;
}
tails[left] = num;
}
return tails.length;
} -> Option A
Quick Check:
Binary search with left < right and tails[mid] < num [OK]
Hint: Use binary search with left < right and tails[mid] < num [OK]
Common Mistakes:
Using wrong binary search boundaries (<= instead of <)
Incorrect mid calculation (ceil instead of floor)
Wrong comparison operators in binary search
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.