Skip to content

Longest Common Subsequence (LCS)

Get access to track progress Get access to bookmark
Asset Pricingx / 34
Behavioral Questionsx / 7
Brain Teasersx / 79
Derivatives Theoryx / 107
Digital Assets - Cryptox / 82
Energy Tradingx / 55
Linear Algebrax / 24
Machine Learningx / 62
Math Questionsx / 50
Probability and Statisticsx / 369
Programmingx / 54
Totalx / 923
You're given two sequences of integers. Your task is to write a function that finds the Longest Common Subsequence (LCS) of the two sequences.
def find_lcs(seq1: list, seq2: list) -> list:

pass

Inputs
  • seq1: A list of integers representing the first sequence.
  • seq2: A list of integers representing the second sequence.
Output
Return a list of integers representing the Longest Common Subsequence of seq1 and seq2.

Example
Given the sequences seq1 = [1, 2, 3, 4, 5] and seq2 = [6, 7, 1, 2, 8], your function should return [1, 2].
# Implementing the Longest Common Subsequence (LCS) using Dynamic Programming

def find_lcs(seq1, seq2):
n = len(seq1)
m = len(seq2)

# Initialize a DP table with zeros
dp = [[0] * (m + 1) for _ in range(n + 1)]

# DP step: fill in the table
for i in range(1, n + 1):
for j in range(1, m + 1):
if seq1[i-1] == seq2[j-1]:
dp[i][j] = dp[i-1][j-1] + 1
else:
dp[i][j] = max(dp[i-1][j], dp[i][j-1])

# Reconstruct the LCS
lcs = []
i, j = n, m
while i > 0 and j > 0:
if seq1[i-1] == seq2[j-1]:
lcs.append(seq1[i-1])
i -= 1
j -= 1
elif dp[i-1][j] > dp[i][j-1]:
i -= 1
else:
j -= 1

return lcs[::-1] # Reverse the LCS to get it in the correct order

# Test the function
seq1 = [1, 2, 3, 4, 5]
seq2 = [6, 7, 1, 2, 8]
print(find_lcs(seq1, seq2)) # Should return [1, 2]
Python scratchpad
# Run Python right here in your browser
def two_sum(nums, target):
    seen = {}
    for i, n in enumerate(nums):
        if target - n in seen:
            return [seen[target - n], i]
        seen[n] = i
    return []

print(two_sum([2, 7, 11, 15], 9))  # [0, 1]
Run Python in your browser

Write and execute code — numpy, pandas & scikit-learn included — right on the question page.

Log in to start coding
Title Category Subcategory Difficulty Status
All Pairs ProgrammingArray and List ProcessingEasy
Example
All Permutations ProgrammingArray and List ProcessingMedium
Unlock with a membership
Arithmetic Mean ProgrammingArray and List ProcessingHard
Unlock with a membership
Binary Search Tree (BST) ProgrammingArray and List ProcessingHard
Unlock with a membership
Counting Inversions ProgrammingArray and List ProcessingHard
Unlock with a membership
Crate Loading ProgrammingArray and List ProcessingEasy
Unlock with a membership
Find the Missing Number ProgrammingArray and List ProcessingEasy
Unlock with a membership
Finding the Largest Possible Number ProgrammingArray and List ProcessingEasy
Unlock with a membership
Longest Increasing Subsequence ProgrammingArray and List ProcessingHard
Unlock with a membership
Majority Element ProgrammingArray and List ProcessingMedium
Unlock with a membership
Maximum Subarray Sum ProgrammingArray and List ProcessingMedium
Unlock with a membership
Search a Rotated Array ProgrammingArray and List ProcessingMedium
Unlock with a membership
Settling Array ProgrammingArray and List ProcessingMedium
Unlock with a membership
Sorting Without Sort() ProgrammingArray and List ProcessingHard
Unlock with a membership
Subarray Sum Equals Zero ProgrammingArray and List ProcessingMedium
Unlock with a membership
Sudoku ProgrammingArray and List ProcessingHard
Unlock with a membership

Please log in to see the discussion.