ProgrammingHardTCS NQT · Accenture
The string "abcabcbb" is scanned with a sliding window that keeps the longest stretch without a repeated character. The procedure below returns that length. What is printed?
The string "abcabcbb" is scanned with a sliding window that keeps the longest stretch without a repeated character. The procedure below returns that length. What is printed?
left = 0; best = 0; last = empty map
for right = 0 to n - 1
if s[right] was seen at index >= left then left = that index + 1
last[s[right]] = right
best = max(best, right - left + 1)
end for
Print best- A. 3
- B. 2
- C. 8
- D. 0
Correct answer: A. 3
The longest window without repeats is abc, of length 3. Subtracting one from the best gives 2, printing the string length 8 is unrelated, and never updating best gives 0.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Scanning "abcab dab" as a string of letters, the procedure below counts the adjacent pairs that read "ab" (both letters next to each…
- Two words are checked as anagrams by comparing letter counts. The procedure below sums the absolute differences between the letter counts…
- The minimum number of insertions or deletions needed to make the string "abcca" a palindrome is len(s) - LPS(s), where LPS is the length…
- The edit distance (Levenshtein, insert/delete/replace each cost 1) between "kitten" and "sitting" is computed by the standard table. What…
- The string " hello world " is split by the Python-style rule that runs of spaces are treated as one separator and empty pieces are…
- The longest common subsequence (LCS) of "ABCBDAB" and "BDCABA" is computed by the standard table. The procedure below prints its length…