ProgrammingVery hardGoldman Sachs OA · Amazon OA
What will be the output of the following pseudocode?
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 from 0 to r - 1)
if (col[k] == c OR abs(col[k] - c) == r - k)
return false
end if
end for
return true
End Function
Procedure place(Integer r)
if (r == n)
count = count + 1
return
end if
for (each c from 0 to n - 1)
if (ok(r, c))
col[r] = c
place(r + 1)
end if
end for
End Procedure
place(0)
Print count- A. 4
- B. 10
- C. 40
- D. 0
Correct answer: A. 4
This backtracking counts ways to put 6 non-attacking queens on a 6 × 6 board. Surprisingly there are only 4 (fewer than the 10 for a 5 × 5 board).
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? (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…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[6] = {3, 2, 2, 3, 4, 3}, k = 0, val = 3 for…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer n = 7, c = 1 for (each…