Coding Challenges: String Manipulation
📂 Phase 6: Mini Projects, Coding Challenges, Interview Preparation (Days 26–30) · Python ProgrammingString manipulation problems are among the most frequently asked coding challenges in early interview rounds — they test comfort with indexing, slicing, and iteration without requiring advanced algorithmic knowledge, making them the natural starting point for structured practice.
Challenge 1: Check If a String Is a Palindrome
def is_palindrome(text):
cleaned = text.lower().replace(" ", "")
return cleaned == cleaned[::-1]
print(is_palindrome("Madam")) # True
print(is_palindrome("racecar")) # True
print(is_palindrome("hello")) # False
print(is_palindrome("nurses run")) # True — ignoring spaces
The cleanest Python solution compares the cleaned string against its own reverse using slicing ([::-1]), avoiding any manual loop entirely.
Challenge 2: Reverse a String Without Using [::-1]
def reverse_string(text):
result = ""
for char in text:
result = char + result # prepend each character
return result
print(reverse_string("Python")) # nohtyP
Interviewers sometimes specifically forbid the slice shortcut to see whether you understand the underlying logic, not just Python's convenient syntax — be ready with both approaches.
Challenge 3: Count the Frequency of Each Character
def char_frequency(text):
freq = {}
for char in text:
freq[char] = freq.get(char, 0) + 1
return freq
print(char_frequency("banana"))
# {'b': 1, 'a': 3, 'n': 2}
The Same Problem, Using collections.Counter
from collections import Counter
print(Counter("banana"))
# Counter({'a': 3, 'n': 2, 'b': 1})
print(Counter("banana").most_common(1))
# [('a', 3)] — the most frequent character, with its count
Challenge 4: Check If Two Strings Are Anagrams
def is_anagram(a, b):
return sorted(a.lower()) == sorted(b.lower())
print(is_anagram("listen", "silent")) # True
print(is_anagram("hello", "world")) # False
Sorting both strings and comparing the results is the simplest correct approach — two strings are anagrams if and only if they contain exactly the same characters in the same quantities, which sorting will always reveal.
Challenge 5: Count Vowels and Consonants
def count_vowels_consonants(text):
vowels = "aeiouAEIOU"
vowel_count = sum(1 for char in text if char in vowels)
consonant_count = sum(1 for char in text if char.isalpha() and char not in vowels)
return vowel_count, consonant_count
v, c = count_vowels_consonants("Hello World")
print(f"Vowels: {v}, Consonants: {c}") # Vowels: 3, Consonants: 7
Challenge 6: Find the First Non-Repeating Character
def first_unique_char(text):
freq = {}
for char in text:
freq[char] = freq.get(char, 0) + 1
for char in text:
if freq[char] == 1:
return char
return None
print(first_unique_char("swiss")) # w
The first loop builds a frequency map; the second loop walks through the string again in its original order, returning the first character whose count is exactly 1.
Time Complexity Summary
| Challenge | Typical Time Complexity |
|---|---|
| Palindrome check | O(n) |
| Character frequency | O(n) |
| Anagram check (via sorting) | O(n log n) |
| First unique character | O(n) |
Interview tip: For nearly every string problem, interviewers want you to state the time complexity of your solution out loud without being asked — even a correct answer feels incomplete to many interviewers if you cannot explain why it runs in O(n) or O(n log n) time.