Preparing for LeetCode basic coding questions for feenyx.

Easy

Palindrome Check

Given a string s, return True if it is a palindrome and False otherwise.

A palindrome reads the same forward and backward.

Examples

  • "racecar" -> True
  • "hello" -> False
  • "a" -> True
  • "" -> True

Notes

  • Assume case-sensitive comparison unless told otherwise.
  • Focus on a clean Python solution.
  • Inputs: string s
  • Outputs: boolean
  • edge cases: empty string, single character

What this tests

  • basic string handling
  • indexing or slicing
  • edge cases
def is_palindrome(string: str) -> bool:
    return string == string[::-1]

list_of_strings = ["racecar", "hello", "a", ""]
for s in list_of_strings:
    print(f"{s!r} -> {is_palindrome(s)}")
'racecar' -> True
'hello' -> False
'a' -> True
'' -> True

Medium

Longest Substring Without Repeating Characters

Given a string s, return the length of the longest substring without repeating characters.

Examples

  • s = "abcabcbb" -> 3 because "abc"
  • s = "bbbbb" -> 1 because "b"
  • s = "pwwkew" -> 3 because "wke"

Constraints

  • 0 <= len(s) <= 10^5
  • s consists of English letters, digits, symbols, and spaces

What this tests

  • sliding window
  • hash map or set usage
  • runtime awareness
  • clean Python implementation

Follow-up

Be ready to explain:

  • why brute force is too slow
  • time complexity: O(n)
  • space complexity: O(k) or O(n) depending on implementation
  • edge cases like empty string and all-duplicate input

⏱️ Time Complexity Cheat Sheet

  • 1 loop → O(n)
  • nested loops → O(n²)
  • two pointers (both only move forward) → O(n)
  • hash map / set lookup → O(1)
  • sorting → O(n log n)

🧠 Quick rules

  • Count total pointer movement, not loops
  • If each element is touched once or twice → O(n)
  • If you compare every pair → O(n²)

🔥 Sliding window (your problem)

  • right moves → n
  • left moves → n 👉 O(n)

Notes:

  • inputs: string s

  • outputs: integer length of longest substring

  • edge cases: empty string, all characters the same

def longest_unique_substring(string: str) -> int:
    """
    Return the length of the longest substring of `s` with all unique characters.
    Uses a sliding window with two pointers (`left`, `right`) and a dictionary
    mapping each character to its most recent index. When a duplicate character
    is encountered within the current window, the left boundary jumps forward
    to one position after the previous occurrence, preserving the invariant
    that the window always contains unique characters.
    This approach ensures each character is processed at most once, yielding
    linear time complexity.

    Edge behavior:
    - Empty string returns 0
    - All identical characters returns 1
    - Supports any ASCII/Unicode characters
    
    Complexity:
    - Time: O(n)
    - Space: O(min(n, alphabet_size))
    possible_strings = set()
    """
    last_seen = {}
    left = 0
    best = 0
    best_start = 0

    # Creating the window with the for loop
    for right, char in enumerate(string):
        # If the character has been seen and is within the window, 
        # we need to move the index. 
        if char in last_seen and last_seen[char] >= left:
            left = last_seen[char] + 1

        # sets the last seen index    
        last_seen[char] = right

        # if the length of the window is greater than the best, then update the best and the best start index
        if right - left + 1 > best:
            best = right - left + 1
            best_start = left

    return best, string[best_start:best_start + best]

string = "abcabcbbe"
    
best, substring_length = longest_unique_substring(string)
print(f"Best length: {best}, Substring indices: {substring_length}")
Best length: 3, Substring indices: abc

Hidden Test Gotcha

Most Frequent Word

Given a string text, return the most frequent word after:

  • converting all text to lowercase
  • removing punctuation
  • splitting on whitespace

If multiple words tie for the highest frequency, return the lexicographically smallest word.

Examples

  • "The cat and the hat." -> "the"
  • "Hi, hi! bye? Bye." -> "bye"

Hidden test gotchas to watch for

  • punctuation attached to words
  • mixed capitalization
  • multiple spaces
  • ties in word frequency
  • empty input after cleanup

What this tests

  • basic text normalization
  • tokenization logic
  • dictionary counting
  • tie-breaking rules
  • careful edge-case handling

Notes: - [^...] -> not these characters - -> word characters (letters, digits, underscore) - -> whitespace characters

# word characters (letters, digits, underscore) # NOT word characters # whitespace (space, tab, newline) # NOT whitespace # digits (0-9) # NOT digits

import re 

string = "Hallelujah! Hallelujah? I have risen, I have risen. Lordt."
lower_case_string = string.lower()

#remove everything that is not a word character or whitespace
clean_string = re.sub(r'[^\w\s]', '', lower_case_string)

cleaned_split = clean_string.split()
Back to top