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 h[6] = {2, 1, 5, 6, 2, 3}, best = 0
for (each i from 0 to 5)
Integer mn = h[i]
for (each j from i to 5)
mn = min(mn, h[j])
best = max(best, mn * (j - i + 1))
end for
end for
Print best- A. 12
- B. 6
- C. 10
- D. 8
Correct answer: C. 10
The loops find the largest rectangle in the histogram: bars 5 and 6 side by side give height 5 × width 2 = 10. Using all six bars gives only 1 × 6 = 6.
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? String stream = "aabcbc" Integer cnt[26] = all 0 Queue q = empty for (each ch in…
- What will be the output of the following pseudocode? Procedure f(Integer n) if (n == 0) return end if Print n f(n - 1) Print n End…
- What will be the output of the following pseudocode? // a trie (prefix tree) starts with only an empty root for each word in {"car"…
- What will be the output of the following pseudocode? // LRU cache of capacity 2: get(k) returns the value or -1; put evicts the least…
- What will be the output of the following pseudocode? Integer g[3][4] = {{1, 1, 1, 0}, {1, 1, 1, 1}, {0, 1, 1, 1}} Integer c = 0 for (each…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Function count7(Integer n) if…