ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode? (code(c) gives a letter's character code; strings are indexed from 0)
What will be the output of the following pseudocode? (code(c) gives a letter's character code; strings are indexed from 0)
String s = "ABCADEFB"
Integer last[26] = all -1
Integer start = 0, best = 0
for (each i from 0 to length(s) - 1)
Integer k = code(s[i]) - code('A')
if (last[k] >= start)
start = last[k] + 1
end if
last[k] = i
best = max(best, i - start + 1)
end for
Print best- A. 8
- B. 5
- C. 7
- D. 6
Correct answer: D. 6
best is the longest window with no repeated letter. At the second A the window restarts after the first A (BCADEF), and at the final B after the first B (CADEFB): both 6 letters. 8 ignores the repeats, 7 sets start = last[k] without the +1, and 5 restarts the window at i instead of after the old copy.
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? // list: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL Node slow = head, fast = head while…
- What will be the output of the following pseudocode? Integer m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} Integer j for (each d from 0 to…
- What will be the output of the following pseudocode? (strings are indexed from 0) Procedure perm(String s, Integer l) // s is passed by…
- What will be the output of the following pseudocode? (>> is right shift and & is bitwise AND; (mask >> i) & 1 is evaluated first) Integer…
- 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…
- 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…