ProgrammingVery hardGoldman Sachs OA · Amazon OA
What will be the output of the following pseudocode? (strings are indexed from 0)
What will be the output of the following pseudocode? (strings are indexed from 0)
Procedure perm(String s, Integer l) // s is passed by value (a copy)
if (l == length(s) - 1)
Print s
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("ABC", 0)- A. ABC ACB BAC BCA CAB CBA
- B. ABC ACB BCA BAC CAB CBA
- C. ABC ACB BAC BCA CBA CAB
- D. ABC BAC CBA ACB BCA CAB
Correct answer: C. ABC ACB BAC BCA CBA CAB
Swap-based generation fixes each letter at the front in turn. Putting C in front swaps A and C, giving CBA, and its sub-call prints CBA then CAB. So the order is ABC ACB BAC BCA CBA CAB — not dictionary order at the end.
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? (>> is right shift and & is bitwise AND; (mask >> i) & 1 is evaluated first) Integer…
- What will be the output of the following pseudocode? Integer n = 6, count = 0 Integer col[n] Function ok(Integer r, Integer c) for (each k…
- What will be the output of the following pseudocode? (i++ gives the old value of i and then adds 1) Integer i = 0, c = 0 while (i++ < 5) c…
- What will be the output of the following pseudocode? (--n subtracts 1 from n and then gives the new value) Integer n = 5, s = 0 do s = s +…
- What will be the output of the following pseudocode? (num[i] - '0' converts a digit character to its value) String num = "79927398713"…
- What will be the output of the following pseudocode? (>> is right shift, & is bitwise AND) Integer bits[0..15], total = 0 bits[0] = 0 for…