Python Coding Interview Questions

# Python program to find common letters between two strings

def find_common_letters(str1, str2):
    # Convert both strings to sets to get unique characters
    set1 = set(str1)
    set2 = set(str2)
    
    # Find the intersection of both sets to get common letters
    common_letters = set1.intersection(set2)
    
    return common_letters

# Example usage
string1 = "hello"
string2 = "world"

# Calling the function and storing the result
common_chars = find_common_letters(string1, string2)

# Displaying the common letters
print("Common letters:", common_chars)
# Python program to count the frequency of words in a string
from collections import Counter

def word_frequency(text):
    # Split the string into words
    words = text.split()
    
    # Use Counter to count the occurrences of each word
    frequency = Counter(words)
    
    return frequency

# Example usage
input_text = "hello world hello everyone"

# Calling the function and storing the result
word_count = word_frequency(input_text)

# Displaying the word frequencies
print("Word frequencies:", word_count)
Scroll to Top