Python Coding Interview Questions with Answers ■■
1■■ Reverse a String
def reverse_string(s):
return s[::-1]
print(reverse_string("hello")) # Output: "olleh"
2■■ Check Palindrome
def is_palindrome(s):
s = [Link](" ", "").lower()
return s == s[::-1]
print(is_palindrome("Race car")) # Output: True
3■■ Find Duplicate Elements in List
from collections import Counter
def find_duplicates(lst):
count = Counter(lst)
return [item for item, freq in [Link]() if freq > 1]
print(find_duplicates([1, 2, 3, 2, 4, 1])) # Output: [1, 2]
4■■ Count Vowels in a String
def count_vowels(s):
return sum(1 for char in [Link]() if char in "aeiou")
print(count_vowels("Python is fun")) # Output: 4
5■■ Find Factorial Using Recursion
def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n - 1)
print(factorial(5)) # Output: 120
6■■ Find the Largest Element in a List
def find_max(lst):
return max(lst)
print(find_max([3, 7, 2, 9, 5])) # Output: 9