ProgrammingHardCapgemini · Infosys pattern
What will be the output of the following pseudocode (length of the longest substring without repeating characters, sliding window)?
What will be the output of the following pseudocode (length of the longest substring without repeating characters, sliding window)?
Function longestUnique(String s)
Map seen = empty map
Integer start = 0, best = 0
for (each i from 0 to length(s) - 1)
Character ch = s[i]
if (ch in seen and seen[ch] >= start)
start = seen[ch] + 1
end if
seen[ch] = i
best = max(best, i - start + 1)
end for
return best
End Function
Print longestUnique("abcabcbb")- A. 4
- B. 2
- C. 3
- D. 5
Correct answer: C. 3
The longest substrings without a repeated character here are 'abc', 'bca', 'cab' etc., each of length 3; no window ever grows to length 4 before a repeat forces the window to shrink.
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 (number of bits needed to represent n in binary)? Function bitsNeeded(Integer n)…
- What will be the output of the following pseudocode (counting trailing zero bits in the binary representation of n)? (All variables are…
- What will be the output of the following pseudocode (recursively reversing a list)? Function reverseList(List lst) if (length(lst) <= 1)…
- What will be the output of the following pseudocode (counting set bits for every number from 0 to n using a DP recurrence)? (>> is right…
- What will be the output of the following pseudocode (reversing the order of words in a sentence)? String s = "the sky is blue" List words…
- What will be the output of the following pseudocode (number of ways to make change for an amount using given coin denominations, counting…