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
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"" -> TrueNotes
What this tests
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^5s consists of English letters, digits, symbols, and spacesWhat this tests
Follow-up
Be ready to explain:
O(n)O(k) or O(n) depending on implementation⏱️ Time Complexity Cheat Sheet
⸻
🧠 Quick rules
⸻
🔥 Sliding window (your problem)
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
Given a string text, return the most frequent word after:
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
What this tests
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