ProgrammingVery hardCAT · MBA placement
The longest common subsequence (LCS) of "ABCBDAB" and "BDCABA" is computed by the standard table. The procedure below prints its length. What is printed?
The longest common subsequence (LCS) of "ABCBDAB" and "BDCABA" is computed by the standard table. The procedure below prints its length. What is printed?
dp[i][j] = dp[i-1][j-1] + 1 if the letters match, else max(dp[i-1][j], dp[i][j-1]) Print dp[m][n]
- A. 3
- B. 2
- C. 4
- D. 6
Correct answer: C. 4
The LCS is BCBA (or BDAB), which has length 4. Ignoring the max over skipped characters undercounts to 3, the longest common contiguous run is only 2 (AB), and the length of the shorter string gives 6.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The number of non-empty substrings of "abcd" (substrings are contiguous pieces, counted by position) is computed by the procedure below…
- The procedure below counts the uppercase letters in the sentence "Hello World From AI". What is printed? count = 0 for each character c if…
- Strings are compared by dictionary order. The procedure below counts how many of ["apple", "mango", "kiwi", "banana", "mangoes"] are…
- A message of digits is decoded by mapping 1 to A, 2 to B, ..., 26 to Z, where a digit string may be split into one- or two-digit groups…
- A polynomial rolling hash of the string "abc" is computed as h = (h 31 + value) mod 1000, starting from h = 0, where value is the position…
- The string "bca" may have exactly one swap of two characters made. The procedure below finds the lexicographically smallest string…