ProgrammingHardInfosys · Cognizant
Array a = [1, 3, 2, 3, 4, 1] is searched for the longest strictly increasing contiguous run. The procedure below returns its length. What is printed?
Array a = [1, 3, 2, 3, 4, 1] is searched for the longest strictly increasing contiguous run. The procedure below returns its length. What is printed?
best = 1; run = 1
for i = 1 to n - 1
if a[i] > a[i-1] then run = run + 1 else run = 1
best = max(best, run)
end for
Print best- A. 3
- B. 5
- C. 6
- D. 2
Correct answer: A. 3
The runs are 1-3 (length 2), 2-3-4 (length 3) and then 1, so the longest is 3. Counting the adjacent pairs gives 5, treating any change as a rise gives 6, and starting the run at 0 gives 2.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Array a = [5, 1, 4, 2, 3, 7]. The procedure below counts the interior local maxima, that is, positions i from 1 to n - 2 whose value is…
- The KMP prefix function pi[i] is the length of the longest proper prefix of s[0..i] that is also a suffix of it. For s = "abacabab" the…
- Pattern "aba" is searched in the text "abababa" with overlapping matches allowed. The procedure below counts the starting positions where…
- For the string s = "forgeeksskeegfor", the procedure below expands around each centre to find the length of the longest palindromic…
- Count the palindromic substrings (substrings of length 1 or more that read the same forwards and backwards, counted by position) of the…
- Run-length encoding replaces each run of equal characters by the character followed by its run length. The procedure below encodes s =…