ProgrammingHardAmazon OA · Infosys SP
What will be the output of the following pseudocode? (substring(s, i, 2) is the 2 characters from index i; sorted(t) puts t's letters in alphabetical order)
What will be the output of the following pseudocode? (substring(s, i, 2) is the 2 characters from index i; sorted(t) puts t's letters in alphabetical order)
String s = "ABBAAB"
Integer c = 0
for (each i from 0 to length(s) - 2)
String t = substring(s, i, 2)
if (sorted(t) == "AB")
c = c + 1
end if
end for
Print c- A. 2
- B. 5
- C. 4
- D. 3
Correct answer: D. 3
The windows of length 2 are AB, BB, BA, AA, AB. Sorting the letters turns BA into AB too, so windows that are anagrams of AB are counted: AB, BA, AB = 3.
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? (strings are indexed from 0) String p = "ABABCABAB" Integer f[9], k = 0 f[0] = 0 for…
- What will be the output of the following pseudocode? Integer a[4] = {1, 2, 3, 4}, res[4], left = 1, right = 1 for (each i from 0 to 3)…
- What will be the output of the following pseudocode? Integer a[5] = {2, 3, -2, 4, -1} Integer mx = a[0], mn = a[0], best = a[0], t for…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[7] = {-7, 1, 5, 2, -4, 3, 0} Integer total = 0…
- What will be the output of the following pseudocode? Integer a[5] = {1, 5, 3, 4, 2}, k = 2, c = 0 for (each i from 0 to 4) for (each j…
- What is the time complexity of the following pseudocode, as a function of n? for (each i from 1 to n) for (each j from 1 to i i) if (j mod…