Data Structures & AlgorithmsModerateProduct Company SDE · sliding window OA

Problem: What is the length of the longest substring without repeating characters in 'pwwkew'? Run the sliding-window code.

Problem: What is the length of the longest substring without repeating characters in 'pwwkew'? Run the sliding-window code.
s = 'pwwkew'
last = {}
l = best = 0
for r, ch in enumerate(s):
    if ch in last and last[ch] >= l:
        l = last[ch] + 1
    last[ch] = r
    best = max(best, r - l + 1)
print(best)
What is printed?
  1. A. 2
  2. B. 4
  3. C. 3
  4. D. 6

Correct answer: C. 3

The longest such substrings are 'wke' and 'kew', each of length 3. The substring 'pwke' is invalid because the w repeats.

More Coding Interview Problems questions

Exams that ask this topic