ProgrammingHardAccenture · Cognizant pattern
What will be the output of the following pseudocode (length of the longest common SUBSTRING, i.e. contiguous, not subsequence)?
What will be the output of the following pseudocode (length of the longest common SUBSTRING, i.e. contiguous, not subsequence)?
Function lcSubstring(String a, String b)
Integer m = length(a), n = length(b)
Integer dp[m+1][n+1] = all zeros
Integer best = 0
for (each i from 1 to m)
for (each j from 1 to n)
if (a[i-1] == b[j-1])
dp[i][j] = dp[i-1][j-1] + 1
best = max(best, dp[i][j])
end if
end for
end for
return best
End Function
Print lcSubstring("abcdxyz", "xyzabcd")- A. 3
- B. 4
- C. 7
- D. 2
Correct answer: B. 4
The longest contiguous run common to both strings is 'abcd', which has length 4 (it must be contiguous, unlike a subsequence, so the matching stops there even though both strings share all 7 characters overall).
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode (counting distinct ordered character pairs (s[i], s[j]) with i<j)? (Strings are…
- What will be the output of the following pseudocode (capitalizing the first letter of each word, manually)? String s = "hello world foo"…
- What will be the output of the following pseudocode (maximum sum of any 4 consecutive elements, sliding window)? (Arrays are indexed from…
- What will be the output of the following pseudocode (reversing an array in groups of size k)? (Arrays are indexed from 0.) Integer arr[8]…
- What will be the output of the following pseudocode? (A square matrix's main diagonal is mat[i][i]; its anti-diagonal is mat[i][n-1-i]. If…
- This function uses a counter that PERSISTS across calls (a static/global variable), starting at 3. What does each of the 7 calls print, in…