ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode? (+ joins strings)
What will be the output of the following pseudocode? (+ joins strings)
String s = "PAYPALISHIRING"
Integer rows = 3, r = 0, step = 1
String line[3] = {"", "", ""}
for (each ch in s)
line[r] = line[r] + ch
if (r == 0) step = 1
if (r == rows - 1) step = -1
r = r + step
end for
Print line[0] + line[1] + line[2]- A. PAHNAPLSIIGYIR
- B. PAYPALISHIRING
- C. PYAIHRNAPLSIIG
- D. PAHNPLSIIGAYIR
Correct answer: A. PAHNAPLSIIGYIR
Characters go down rows 0, 1, 2 and back up 1, 0, … in a zigzag. Row 0: P A H N; row 1: A P L S I I G; row 2: Y I R. Joined: PAHNAPLSIIGYIR.
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? (Arrays are indexed from 0) Integer a[5] = {1, 3, 5, 4, 2}, n = 5 Integer i = n - 2…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[9] = {3, 2, 3, 1, 2, 4, 5, 5, 6}, k = 4 sort a…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Function canSplit(Integer a[]…
- What will be the output of the following pseudocode? Integer g[3][3] = {{1, 3, 1}, {1, 5, 1}, {4, 2, 1}} for (each i from 0 to 2) for…
- What will be the output of the following pseudocode? Function rob(Integer a[], Integer lo, Integer hi) Integer inc = 0, exc = 0, t for…
- What will be the output of the following pseudocode? Integer cost[3] = {10, 15, 20} Integer a = 0, b = 0, c // cheapest cost to stand on…