7. Algorithm, Question, Explanation, Solution, at toptal.com : How would you, in general terms, describe dynamic programming? As an example, how would you find the length of the longest common subsequence of elements in two arrays by using this method? Use C++ .
By: Chrysanthus Date Published: 30 Oct 2025
Description of Dynamic Programming
Dynamic programming is a paradigm for solving optimization problems. It consists of finding solutions for intermediate subproblems, which can be stored and reused for solving the actual problem. Dynamic programming is the best approach for difficult problems that always become trivial once we know the solution for a slightly easier instance of that problem - the intermediate subproblem. Dynamic programming solutions are best represented by a recursive relation, and easily implemented.If the intermediate subproblems are not overlapping, then we have just a case of Divide and Conquer.
Many dynamic programming problems involves a two dimensional array, also called a matrix or a table. An attempt to deduce a logical pattern in the content of the 2D array is made, before the code of few number of operations, is written from the pattern.
Lesson
The longest common subsequence (LCS) is defined as the longest subsequence that is common to all the given sequences, provided that the elements of the subsequence are not required to occupy consecutive positions within the original sequences. The order of the elements should also be maintained.Illustration
Longest Common Subsequence Examples
Output: 2
Explanation: The longest subsequence (lcs) which is present in both strings is "ac".
str1 = "aggtab"; str2 = "gxtxayb"
Output: 4
Explanation: The longest common subsequence (lcs) which is present in both strings is "gtab".
str1 = "abc"; str2 = "cba"
Output: 1
Explanation: There are three longest common subsequences of length 1, "a", "b" and "c".
Problem Illustration
Consider the strings: "sandal" with M number of characters, equal to 6, and the string "bananas" with N number of characters equal to 7. Each of these strings is considered an array.The longest common subsequence of characters is "ana". For "sandal", 'd' is skipped.
The Two Dimensional Dynamic Programming (dp) Array
The dp array is:b a n a n a s
i/j 0 1 2 3 4 5 6 7
0 0 0 0 0 0 0 0 0
s 1 0 0 0 0 0 0 0 1
a 2 0 0 1 1 1 1 1 1
n 3 0 0 1 2 2 2 2 2
d 4 0 0 1 2 2 2 2 2
a 5 0 0 1 2 3 3 3 3
l 6 0 0 1 2 3 3 3 3
Explanation of the 2D dp Array
"b a n a n a s"
"s a n d a l"
The Code
b(0) a(1) n(2) a(3) n(4) a(5) s(6)
i/j 0 1 2 3 4 5 6 7
0 0 0 0 0 0 0 0 0
s(0) 1 0 0 0 0 0 0 0 1
a(1) 2 0 0 1 1 1 1 1 1
n(2) 3 0 0 1 2 2 2 2 2
d(3) 4 0 0 1 2 2 2 2 2
a(4) 5 0 0 1 2 3 3 3 3
l(5) 6 0 0 1 2 3 3 3 3
The Logical Pattern
There are two rules to deduce here, which are alternatives to one another.Rule 1
Let str1 identify "sandal" and str2 identify "bananas". If for a cell in the 2-dimensional array, the corresponding letters in the rows header ("sandal") and columns header ("bananas") are the same, then in order to obtain the number (answer) for that cell, go to the immediate left-top cell; get the number there and add 1 to it. This is true for the following cells:
For str1[0] = str2[6] = 's' , dp[1][7] = 1 = dp[0][6] + 1 .
For str1[1] = str2[1] = 'a' , dp[2][2] = 1 = dp[1][1] + 1 .
For str1[1] = str2[3] = 'a' , dp[2][4] = 1 = dp[1][3] + 1 .
For str1[1] = str2[5] = 'a' , dp[2][6] = 1 = dp[1][5] + 1 .
For str1[2] = str2[2] = 'n' , dp[3][3] = 2 = dp[2][2] + 1 .
For str1[2] = str2[4] = 'n' , dp[3][5] = 2 = dp[2][4] + 1 .
For str1[4] = str2[1] = 'a' , dp[5][2] = 1 = dp[4][1] + 1 .
For str1[4] = str2[3] = 'a' , dp[5][4] = 3 = dp[4][3] + 1 .
For str1[4] = str2[5] = 'a' , dp[5][6] = 3 = dp[4][5] + 1 .
Rule 2
The Program
#include <algorithm> //for max() function
#include <string>
using namespace std;
int lcs(string str1, string str2, int M, int N) { //M for str1 and N for str2
int dp[M+1][N+1]; //M for rows and N for columns
for (int i=0; i<(M+1); i++) //initialize first column
dp[i][0] = 0;
for (int j=0; j<(N+1); j++) //initialize first row
dp[0][j] = 0;
for (int i=1; i<(M+1); i++) {
for (int j=1; j<(N+1); j++) {
if (str1[i-1] == str2[j-1])
dp[i][j] = dp[i-1][j-1] + 1;
else
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
return dp[M][N];
}
int main()
{
string str1 = "sandal";
string str2 = "bananas";
int ret = lcs(str1, str2, 6, 7);
cout << ret << endl;
return 0;
}
3
The time complexity for the solution is O(MxN). The space complexity is also given as O(MxN).
Thanks for reading.