ProgrammingModerateAmazon OA · TCS NQT
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a[6] = {1, 2, 1, 3, 2, 1}
Map cnt = empty
Integer pairs = 0
for (each i from 0 to 5)
pairs = pairs + cnt[a[i]] // cnt of an unseen value is 0
cnt[a[i]] = cnt[a[i]] + 1
end for
Print pairs- A. 6
- B. 3
- C. 4
- D. 5
Correct answer: C. 4
Each element pairs with every earlier equal element. The three 1's give C(3, 2) = 3 pairs and the two 2's give 1: pairs = 4.
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? Integer move = 0 Procedure hanoi(Integer n, Char a, Char b, Char c) if (n == 0)…
- What is the time complexity of the following pseudocode, as a function of n? for (each i from 1 to n) j = 1 while (j < i) j = j 2 end…
- What is the time complexity of the following pseudocode on an array of n elements? Stack st = empty for (each i from 0 to n - 1) while (st…
- What will be the output of the following pseudocode? (c ? x : y evaluates x if c is true, otherwise y) Integer marks[6] = {92, 75, 60, 88…
- What will be the output of the following pseudocode? (continue in a do-while loop goes straight to the while test) Integer x = 1, c = 0 do…
- What will be the output of the following pseudocode? (s[i] - '0' turns a digit character into its value; strings are indexed from 0)…