ProgrammingHardAmazon OA · Infosys SP
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
String s = "ABCAB"
Integer cnt[26] = all 0, total = 0
for (each ch in s)
cnt[ch] = cnt[ch] + 1
total = total + cnt[ch]
end for
Print total- A. 5
- B. 7
- C. 9
- D. 15
Correct answer: B. 7
When a letter is seen for the k-th time it can end k substrings that start with the same letter (including itself). So total counts substrings whose first and last letters match: A: 1 + 2, B: 1 + 2, C: 1, total 7 (A, B, C, A, B, ABCA, BCAB).
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? String s = "COMPUTER" Integer i = 0, j = length(s) - 1 while (i < j) while (i < j AND…
- What will be the output of the following pseudocode? Function isSub(String a, String b) // is b a subsequence of a? Integer j = 0 for…
- What will be the output of the following pseudocode? String s = "ABBACA" Stack st = empty for (each ch in s) if (st not empty AND top(st)…
- What will be the output of the following pseudocode? Integer a[6] = {1, 2, 3, 4, 5, 6} Integer i = 0, j = 5 while (i < j) if (a[i] mod 2…
- What will be the output of the following pseudocode? Integer a[5] = {0, 1, 0, 3, 12}, k = 0 for (each i from 0 to 4) if (a[i] != 0)…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[6] = {-1, 0, 1, 2, -1, -4} sort a // {-4, -1…