ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a[8] = {10, 9, 2, 5, 3, 7, 101, 18}
Integer L[8], best = 0
for (each i from 0 to 7)
L[i] = 1
for (each j from 0 to i - 1)
if (a[j] < a[i] AND L[j] + 1 > L[i])
L[i] = L[j] + 1
end if
end for
best = max(best, L[i])
end for
Print best- A. 4
- B. 3
- C. 5
- D. 6
Correct answer: A. 4
L[i] is the longest strictly increasing subsequence ending at a[i]. One longest chain is 2, 5, 7, 101 (or 2, 3, 7, 18), so best = 4.
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? (strings are indexed from 0) String x = "SUNDAY", y = "SATURDAY" Integer…
- What will be the output of the following pseudocode? Integer a[6] = {5, 5, 10, 100, 10, 5} Integer incl = 0, excl = 0, t for (each i from…
- An array of n elements is ALREADY sorted in increasing order. What is the time complexity of this pseudocode on it? for (each i from 1 to…
- 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 will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer n = 7086351, s = 0, d…
- What will be the output of the following pseudocode? Procedure upd(Integer arr[], Integer n) // arrays are passed by reference arr[0] =…