60 Python Core Interview Pattern Questions
1. Reverse a String
s = "hello world"
# Method 1: Logic-based
rev = ""
for ch in s:
rev = ch + rev
print(rev)
# Method 2: Inbuilt
print(s[::-1])
2. Find the Longest Word
s = "Python is an easy language"
# Logic-based
words = [Link]()
max_word = ""
for word in words:
if len(word) > len(max_word):
max_word = word
print(max_word)
# Inbuilt
print(max(words, key=len))
3. Count the Occurrence of Each Character
s = "hello"
count_dict = {}
# Logic
for ch in s:
count_dict[ch] = count_dict.get(ch, 0) + 1
print(count_dict)
# Inbuilt
from collections import Counter
print(Counter(s))
4. Check if a String is Palindrome
s = "madam"
print(s == s[::-1])
5. Remove Duplicates from a String
s = "banana"
res = ""
60 Python Core Interview Pattern Questions
for ch in s:
if ch not in res:
res += ch
print(res)
# Alternate: using set but loses order
print("".join(set(s)))