ProgrammingHardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer printed = 0
Procedure perm(String s, Integer l) // s is passed by value
if (l == length(s) - 1)
printed = printed + 1
return
end if
for (each i from l to length(s) - 1)
swap(s[l], s[i])
perm(s, l + 1)
swap(s[l], s[i])
end for
End Procedure
perm("AAB", 0)
Print printed- A. 6
- B. 3
- C. 2
- D. 9
Correct answer: A. 6
Without a check for repeated letters, the swap method produces all 3! = 6 orders, treating the two A's as different, so AAB, ABA and BAA each appear twice. printed = 6 although only 3 strings are distinct.
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 t[4][4] = {{2}, {3, 4}, {6, 5, 7}, {4, 1, 8, 3}} // row r has r + 1 numbers…
- What will be the output of the following pseudocode? Integer a = 20, b = 6, c = 0 while (a > b) a = a - 3 b = b + 2 c = c + 1 end while…
- What will be the output of the following pseudocode? Integer r[3][4] = {{1, 2, 3, 4}, {2, 4, 6, 8}, {4, 2, 5, 1}} Integer c = 0 for (each…
- What will be the output of the following pseudocode? // list: 10 -> 20 -> 30 -> 40 -> 50 -> NULL Node fast = head, slow = head Integer k =…
- What will be the output of the following pseudocode? Integer a[7] = {10, 5, 3, 4, 3, 5, 6} Set seen = empty Integer first = -1 for (each i…
- 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 n) if (i == j)…