ProgrammingHardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer count = 0
String last = ""
Procedure gen(String s, Integer n)
if (length(s) == n)
count = count + 1
last = s
return
end if
gen(s + "0", n)
if (length(s) == 0 OR s[length(s) - 1] == '0')
gen(s + "1", n)
end if
End Procedure
gen("", 4)
Print count, last- A. 16 1111
- B. 5 1010
- C. 8 1010
- D. 8 0101
Correct answer: C. 8 1010
gen builds binary strings of length 4 with no two adjacent 1's, trying 0 before 1, so they come out in increasing order: 0000, 0001, 0010, 0100, 0101, 1000, 1001, 1010. count = 8 and the last one is 1010.
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 p[4] = {2, 0, 3, 1} // coefficients of 2x³ + 0x² + 3x + 1 Integer x = 3, m =…
- What is the time complexity of the following pseudocode, as a function of n? Procedure f(Integer n) if (n <= 1) return end if for (each i…
- What is the time complexity of the following pseudocode, as a function of n? for (i = 1; i i <= n; i = i + 1) for (j = 1; j <= n; j = j 2)…
- What is the time complexity of the following pseudocode, as a function of n? Function f(Integer n) if (n <= 1) return 1 end if return f(n…
- What will be the output of the following pseudocode? (OR stops evaluating once the left side is true; ++c adds 1 to c and then gives the…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer nxt[8] = {1, 2, 3, 4, 5, 6, 7, 3} Integer slow =…