What Python coding interview questions and answers to prepare?
Preparing for a Python coding interview involves familiarizing yourself with a range of questions that test your understanding of Python concepts, data structures, algorithms, and problem-solving skills. Here are some common Python coding interview questions along with their explanations and answers to help you prepare:
1. Reverse a String
Question: Write a function to reverse a string.
Answer:
def reverse_string(s): return s[::-1] # Example usage print(reverse_string("hello")) # Output: "olleh"
2. Check for Palindrome
Question: Write a function that checks if a string is a palindrome.
Answer:
def is_palindrome(s): return s == s[::-1] # Example usage print(is_palindrome("racecar")) # Output: True print(is_palindrome("hello")) # Output: False
3. Find the Largest Element in a List
Question: Write a function that finds the largest element in a list.
Answer:
def find_largest(numbers): return max(numbers) # Example usage print(find_largest([1, 2, 3, 4, 5])) # Output: 5
4. Fibonacci Sequence
Question: Write a function to generate the Fibonacci sequence up to n
terms.
Answer:
def fibonacci(n): sequence = [0, 1] while len(sequence) < n: sequence.append(sequence[-1] + sequence[-2]) return sequence[:n] # Example usage print(fibonacci(10)) # Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
5. Sum of All Elements in a List
Question: Write a function to calculate the sum of all elements in a list.
Answer:
def sum_list(numbers): return sum(numbers) # Example usage print(sum_list([1, 2, 3, 4, 5])) # Output: 15
6. Find Duplicates in a List
Question: Write a function to find duplicates in a list.
Answer:
def find_duplicates(nums): seen = set() duplicates = set() for num in nums: if num in seen: duplicates.add(num) seen.add(num) return list(duplicates) # Example usage print(find_duplicates([1, 2, 3, 2, 4, 5, 3])) # Output: [2, 3]
7. Sort a List
Question: Write a function to sort a list.
Answer:
def sort_list(lst): return sorted(lst) # Example usage print(sort_list([5, 3, 6, 2, 8])) # Output: [2, 3, 5, 6, 8]
8. Count Vowels in a String
Question: Write a function to count the number of vowels in a string.
Answer:
def count_vowels(s): vowels = "aeiouAEIOU" return sum(1 for char in s if char in vowels) # Example usage print(count_vowels("hello world")) # Output: 3
9. Merge Two Sorted Lists
Question: Write a function to merge two sorted lists into one sorted list.
Answer:
def merge_sorted_lists(list1, list2): return sorted(list1 + list2) # Example usage print(merge_sorted_lists([1, 3, 5], [2, 4, 6])) # Output: [1, 2, 3, 4, 5, 6]
10. Check if a Number is Prime
Question: Write a function to check if a number is prime.
Answer:
def is_prime(n): if n <= 1: return False for i in range(2, int(n**0.5) + 1): if n % i == 0: return False return True # Example usage print(is_prime(11)) # Output: True print(is_prime(4)) # Output: False
Additional Resources for Preparation
- LeetCode - Offers a wide variety of coding problems for practice.
- HackerRank - Provides coding challenges in Python and other languages.
- GeeksforGeeks - Contains examples and explanations of Python coding problems.
Practicing these types of questions will enhance your problem-solving skills and prepare you for technical interviews focused on Python.
GET YOUR FREE
Coding Questions Catalog