Sitemap

Common Python Coding Problems — Part 1

2 min readDec 23, 2024

--

Press enter or click to view image in full size

In this article, we will solve some common coding problems in Python. You can use this list to practice interview questions.

FizzBuzz Problem — Python

def fizzbuzz(n):
for i in range(1, n + 1):
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)

# Example usage
fizzbuzz(100)

Two Sum Problem — Python

def two_sum(nums, target):
num_to_index = {}

for index, num in enumerate(nums):
complement = target - num
if complement in num_to_index:
return [num_to_index[complement], index]
num_to_index[num] = index

return None # Return None if no solution is found

# Example usage
input_list = [2, 7, 11, 15]
target_sum = 9
result = two_sum(input_list, target_sum)
print(result) # Output: [0, 1]

Get CodeyMaze’s stories in your inbox

Join Medium for free to get updates from this writer.

Palindrome Checker — Python

def is_palindrome(value):
# Convert the input to string to handle both strings and numbers
str_value = str(value)

# Check if the string is equal to its reverse
return str_value == str_value[::-1]

# Example usage
print(is_palindrome("madam")) # Output: True
print(is_palindrome(121)) # Output: True
print(is_palindrome("hello")) # Output: False
print(is_palindrome(12321)) # Output: True

Anagram Checker — Python

def are_anagrams(str1, str2):
# Remove spaces and convert to lowercase
str1 = str1.replace(" ", "").lower()
str2 = str2.replace(" ", "").lower()

# Sort the characters of both strings and compare
return sorted(str1) == sorted(str2)

# Example usage
string1 = "listen"
string2 = "silent"
result = are_anagrams(string1, string2)
print(f"Are '{string1}' and '{string2}' anagrams? {result}")

Reverse a String — Python

# Original string
original_string = "Hello, World!"

# Reversed string using slicing
reversed_string = original_string[::-1]

print(reversed_string)

Find Missing Elements in Array — Python

arr = [1, 2, 3, 4, 5, 7, 6, 10]
full_range = set(range(min(arr), max(arr) + 1))
missing = list(full_range - set(arr))
missing.sort()
print(missing)

Click here to view more problems like this!

--

--

CodeyMaze
CodeyMaze

Written by CodeyMaze

Crafting Solutions Through Code & Words https://codeymaze.com Feel free to follow me :)