0Pricing
Coding Interview Prep · Lesson

Longest Substring Without Repeats

Track last-seen positions in a window.

Longest Substring Without Repeats is a free Coding Interview Prep lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

A Classic Window Problem

Find the longest substring with no repeated character. It is a sliding-window favorite that shows up across nearly every judge. 🔤

The Brute-Force Trap

Checking every substring for duplicates costs about O(n^2) or worse. For long strings that is far too slow, so a smarter sweep is needed.

Window of Unique Characters

Keep a window that always holds distinct characters. Expand on the right, and when a repeat appears, shrink from the left until it is gone.

Remember Last Positions

Store each character's last index in a dictionary. This lets you instantly know where a repeat was last seen as you scan.

last = {}
left = 0
best = 0

Scan Each Character

Loop with right over the string, reading both the index and the character at each step. This drives the window forward one position at a time.

for right, ch in enumerate(s):

Jump the Left Pointer

If the character was seen inside the current window, move left to just past its last position. That removes the duplicate in one move.

    if ch in last and last[ch] >= left:
        left = last[ch] + 1

Update and Measure

Record this character's new position, then the window from left to right is duplicate-free. Its length is right minus left plus one.

    last[ch] = right
    best = max(best, right - left + 1)

Why the Guard Matters

The last[ch] >= left check is essential. Without it, an old position outside the window would wrongly drag left backward.

Linear Time, Linear Space

Each character is visited once and left only moves forward, so the scan is O(n). The dictionary uses space for the distinct characters.

Edge Cases to Cover

An empty string answers zero, and a string of one repeated letter answers one. Verify both before submitting to dodge a sneaky WA.

The Reusable Pattern

The last-seen map plus a jumping left pointer generalizes to many distinctness problems, like windows with at most one repeat.

Quick Check

You track each character's last index while scanning for the longest unique substring.

Recap

Slide a window of unique characters, store each last position, and jump left past repeats. This solves the classic problem in O(n). ✅

Frequently asked questions

Is the “Longest Substring Without Repeats” lesson free?

Yes — the full text of “Longest Substring Without Repeats” is free to read here on the web, and the Coding Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO.

What will I learn in “Longest Substring Without Repeats”?

Track last-seen positions in a window. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Coding Interview Prep?

No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Longest Substring Without Repeats” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Coding Interview Prep lesson?

Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Fixed-Size Window Sums
  2. Variable Window with Two Pointers
  3. Longest Substring Without Repeats
  4. Count Windows That Satisfy a Rule
← Back to Coding Interview Prep