ProgrammingHardAccenture · Cognizant pattern
What will be the output of the following pseudocode (maximum sum of any window of size k)? (Arrays are indexed from 0.)
What will be the output of the following pseudocode (maximum sum of any window of size k)? (Arrays are indexed from 0.)
Integer arr[6] = {2, 1, 5, 1, 3, 2}
Integer k = 3
Integer window = arr[0] + arr[1] + arr[2]
Integer best = window
for (each i from k to length(arr) - 1)
window = window + arr[i] - arr[i - k]
best = max(best, window)
end for
Print best- A. 9
- B. 8
- C. 7
- D. 10
Correct answer: A. 9
The sliding window sums are: {2,1,5}=8, {1,5,1}=7, {5,1,3}=9, {1,3,2}=6. The maximum among these is 9.
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 (counting inversions, brute force)? (Arrays are indexed from 0.) Integer arr[5] = {2…
- What will be the output of the following pseudocode (counting pairs summing to a target, brute force)? (Arrays are indexed from 0.)…
- What will be the output of the following pseudocode (counting pattern occurrences, allowing overlaps)? Function countOcc(String text…
- What will be the output of the following pseudocode (manually parsing a signed integer string)? Function strToInt(String s) Integer sign =…
- What will be the output of the following pseudocode (counting trailing zeros in n factorial, via counting factors of 5)? (All variables…
- What will be the output of the following pseudocode (fast exponentiation)? (All variables are integers, so / is integer division.) Integer…