Coding Medium Software Engineer Frontend Backend Full Stack

Find the longest substring without repeating characters in a given string `s`.

Practise this question with AI feedback

Record your spoken answer or type your response to receive immediate scoring and STAR improvements.

💡 Suggested Approach & Sample Answer

Optimal Sliding Window with Hash Map:
- Expand a window with a right pointer while using a hash map of last seen indices to jump the left pointer past duplicates.

def length_of_longest_substring(s: str) -> int:
    char_index_map = {}
    max_length = 0
    left = 0
    for right, char in enumerate(s):
        if char in char_index_map and char_index_map[char] >= left:
            left = char_index_map[char] + 1
        char_index_map[char] = right
        max_length = max(max_length, right - left + 1)
    return max_length

Complexity & Key Logic:
- Time Complexity: O(N) single-pass traversal of string length N.
- Space Complexity: O(min(N, M)), where M is alphabet size (at most 128 for ASCII or 26 for lowercase).
- Algorithm Details:
- Track the last seen index of each character.
- If char was seen at or after left, duplicate detected: jump left to char_index_map[char] + 1.
- Update character's newest index and calculate max_length = max(max_length, right - left + 1).

🎯 What Interviewers Listen For

How would you answer this in a real interview?

Use our free AI mock practice tool to test your response, analyze speaking pacing, and refine your STAR structure.

🔗 Related Coding Questions