Python Programming Mastery Course Advanced

Perfect — LIST MASTERY – DAY 2 is where you win coding interviews.
Today is pure problem-solving with patterns, all grounded in list internals you learned on Day 1.


🔥 LIST MASTERY — DAY 2

Two Pointers, Sliding Window & In-Place Interview Problems

Image
Image
Image
Image

🧠 DAY 2 GOAL

By the end of today, you will:

  • Instantly recognize array/list problem patterns
  • Write in-place solutions (huge interview signal)
  • Avoid extra memory traps
  • Explain time & space tradeoffs confidently

🧩 CORE LIST PATTERNS (MEMORIZE)

PatternUsed For
Two PointersReverse, partition, palindrome
Sliding WindowSubarrays, max/min windows
In-place IndexingRearrangement
Hash SetDuplicates
Prefix SumRange queries
GreedyMoves / swaps

If you identify the pattern → solution becomes mechanical.


1️⃣ TWO POINTER PATTERN (MOST IMPORTANT)

Used when:

  • You must work in place
  • Comparing ends
  • Removing / partitioning

🔥 Q1. Reverse a List (IN-PLACE)

[1, 2, 3, 4] → [4, 3, 2, 1]

Optimal

def reverse_list(lst):
    left, right = 0, len(lst) - 1
    while left < right:
        lst[left], lst[right] = lst[right], lst[left]
        left += 1
        right -= 1

✔ O(n)
✔ O(1) extra space

🧠 Interview note:
lst[::-1] ❌ (creates new list)


🔥 Q2. Move Zeros to End (Classic)

[0,1,0,3,12] → [1,3,12,0,0]

Optimal (Two pointers)

def move_zeros(lst):
    write = 0
    for read in range(len(lst)):
        if lst[read] != 0:
            lst[write], lst[read] = lst[read], lst[write]
            write += 1

✔ Preserves order
✔ In-place


2️⃣ SLIDING WINDOW ON LISTS

Used when:

  • Subarray
  • Continuous window
  • Max / min / sum
  • Fixed or variable size

🔥 Q3. Maximum Sum Subarray of Size K

[2,1,5,1,3,2], k=3 → 9

Optimal

def max_sum_subarray(lst, k):
    window_sum = sum(lst[:k])
    max_sum = window_sum

    for i in range(k, len(lst)):
        window_sum += lst[i] - lst[i-k]
        max_sum = max(max_sum, window_sum)

    return max_sum

✔ O(n)
✔ No nested loops


🔥 Q4. Longest Subarray with Sum ≤ K

Pattern:

  • Sliding window
  • Shrink when invalid

Interviewers love this.


3️⃣ IN-PLACE REMOVAL (VERY COMMON)

🔥 Q5. Remove Element In-Place

nums = [3,2,2,3], val=3 → length=2

Optimal

def remove_element(nums, val):
    write = 0
    for read in range(len(nums)):
        if nums[read] != val:
            nums[write] = nums[read]
            write += 1
    return write

🧠 Why not remove()?

  • O(n) each call
  • Skips elements
  • Bad in interviews

4️⃣ DUPLICATES & ORDER

🔥 Q6. Remove Duplicates (Preserve Order)

[1,2,2,3,1] → [1,2,3]

Correct

def remove_duplicates(lst):
    seen = set()
    result = []
    for x in lst:
        if x not in seen:
            seen.add(x)
            result.append(x)
    return result

✔ O(n)
✔ Stable


🔥 Q7. Remove Duplicates In-Place (Sorted List)

[1,1,2,2,3] → [1,2,3]

Optimal

def remove_duplicates_sorted(lst):
    write = 1
    for read in range(1, len(lst)):
        if lst[read] != lst[read-1]:
            lst[write] = lst[read]
            write += 1
    return write

5️⃣ ROTATION PROBLEMS (INTERVIEW FAVORITE)

🔥 Q8. Rotate List Right by K

[1,2,3,4,5], k=2 → [4,5,1,2,3]

Optimal (3 reversals)

def rotate(lst, k):
    n = len(lst)
    k %= n

    def reverse(i, j):
        while i < j:
            lst[i], lst[j] = lst[j], lst[i]
            i += 1
            j -= 1

    reverse(0, n-1)
    reverse(0, k-1)
    reverse(k, n-1)

✔ O(n)
✔ O(1) space


6️⃣ MERGING LISTS

🔥 Q9. Merge Two Sorted Lists

[1,3,5], [2,4,6]

Clean

def merge(a, b):
    i = j = 0
    res = []
    while i < len(a) and j < len(b):
        if a[i] < b[j]:
            res.append(a[i]); i += 1
        else:
            res.append(b[j]); j += 1
    res.extend(a[i:])
    res.extend(b[j:])
    return res

🔥 COMMON INTERVIEW TRAPS (LISTS)

❌ Using slicing in loops

lst[i:i+3]  # O(k) each time

❌ Using remove() repeatedly

while x in lst:
    lst.remove(x)  # O(n²)

❌ Using extra lists when in-place is asked

Interviewers penalize this.


🧠 LIST INTERVIEW MENTAL MAP

When you see a list problem:

  1. In-place required? → Two pointers
  2. Subarray? → Sliding window
  3. Sorted? → Exploit order
  4. Membership heavy? → Set
  5. Rotation? → Reverse trick

Say this out loud → interviewer confidence boost.


📝 DAY 2 ASSIGNMENT (MANDATORY)

1️⃣ Predict output (NO RUNNING)

lst = [1,0,2,0,3]
move_zeros(lst)
print(lst)

2️⃣ Coding (IMPORTANT)

def product_except_self(nums):
    """
    LeetCode classic
    No division
    O(n)
    """

3️⃣ Explain (Interview style)

Why is this bad?

for x in lst:
    if x == 0:
        lst.remove(x)

🔜 NEXT — LIST MASTERY DAY 3 🔥

DAY 3

  • Prefix sums
  • Kadane’s algorithm
  • Advanced in-place tricks
  • 25 rapid-fire list interview questions
  • Speed vs clarity decisions

👉 Say “CONTINUE LIST MASTERY – DAY 3” when ready.