Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Top 25 Python Coding Questions for Interview

The document contains a collection of Python coding questions and answers, categorized into beginner, intermediate, and advanced levels. It includes various programming tasks such as finding maximum values, counting vowels, and implementing algorithms like binary search. Additionally, it provides tips for succeeding in technical interviews and emphasizes the importance of practice and understanding coding concepts.

Uploaded by

status fellows
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Top 25 Python Coding Questions for Interview

The document contains a collection of Python coding questions and answers, categorized into beginner, intermediate, and advanced levels. It includes various programming tasks such as finding maximum values, counting vowels, and implementing algorithms like binary search. Additionally, it provides tips for succeeding in technical interviews and emphasizes the importance of practice and understanding coding concepts.

Uploaded by

status fellows
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

result = str1 + " " + str2

print(result) #Hello World

4. Write a Python program to find the maximum of three


numbers
def max_of_three(a, b, c):
return max(a, b, c)

print(max_of_three(1, 2, 3)) # 3

5. Write a Python program to count the number of vowels


in a string
def count_vowels(s):
return sum(1 for char in s if char.lower() in 'aeiou')

print(count_vowels("Hello World")) # 3

6. Write a Python program to calculate the factorial of a


number
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
print(factorial(5)) # 120

7. Write a Python code to convert a string to an integer


str_num = "12345"
int_num = int(str_num)
print(int_num) # 12345

8. Write a Python program to calculate the area of a


rectangle
def area_of_rectangle(length, width):
return length * width

print(area_of_rectangle(5, 3)) # 15
Intermediate Level Python Coding Question &
Answers
Here, are the Python coding questions and answers for the intermediate level:
9. Write a Python code to merge two dictionaries
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
merged = {**dict1, **dict2}
print(merged) #{'a': 1, 'b': 3, 'c': 4}

10. Write a Python program to find common elements in


two lists
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
common = list(set(list1) & set(list2))
print(common) #[3, 4]

11. Write a Python code to remove duplicates from a list


list1 = [1, 2, 2, 3, 4, 4]
unique_list1 = list(set(list1))
print(unique_list1) #[1, 2, 3, 4]

12. Write a Python code to check if a string is a


palindrome
def is_palindrome(s):
return s == s[::-1]

print(is_palindrome("radar")) # True
print(is_palindrome("hello")) # False

13. Write a Python program to find the longest word in a


sentence
def longest_word(sentence):
words = sentence.split()
return max(words, key=len)
print(longest_word("The fox jumps over the lazy dog")) # jumps

14. Write a Python code to find the first non-repeating


character in a string
def first_non_repeating_char(s):
char_count = {}
for char in s:
char_count[char] = char_count.get(char, 0) + 1
for char in s:
if char_count[char] == 1:
return char
return None

print(first_non_repeating_char("nxtwave")) # n

15. Write a Python code to count the number of


uppercase letters in a string
def count_uppercase(s):
return sum(1 for char in s if char.isupper())

print(count_uppercase("Nxtwave")) # 1

Advanced Level Python Coding Question &


Answers
Here, are the most frequently asked advanced Python coding questions in the technical interviews:
16. Write a Python code to implement a binary search
algorithm
def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] < target:
low = mid + 1
elif arr[mid] > target:
high = mid - 1
else:
return mid
return -1

print(binary_search([1, 2, 3, 4, 5], 3)) # 2


17. Write a Python code to implement a function to flatten
a nested list
def flatten(nested_list):
flat_list = []
for item in nested_list:
if isinstance(item, list):
flat_list.extend(flatten(item))
else:
flat_list.append(item)
return flat_list

print(flatten([1, [2, [3, 4], 5], 6])) # [1, 2, 3, 4, 5, 6]

18. Write a Python program to check if a binary tree is


balanced
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None

def is_balanced(root):
if not root:
return True

def height(node):
if not node:
return 0
return 1 + max(height(node.left), height(node.right))

return abs(height(root.left) - height(root.right)) <= 1

root = Node(1)
root.left = Node(2)
root.right = Node(3)
print(is_balanced(root)) # True

19. Write a Python code to find the maximum subarray


sum
def max_subarray_sum(arr):
max_sum = current_sum = arr[0]
for num in arr[1:]:
current_sum = max(num, current_sum + num)
max_sum = max(max_sum, current_sum)
return max_sum

print(max_subarray_sum([-2, 1, -3, 4, -1, 2, 1, -5, 4])) # 6


20. Write a Python program to find the intersection of two
sets
def intersection(set1, set2):
return set1 & set2

print(intersection({1, 2, 3}, {2, 3, 4})) # {2, 3}

21. Write a Python code to implement a simple calculator


def calculator(a, b, operation):
if operation == 'add':
return a + b
elif operation == 'subtract':
return a - b
elif operation == 'multiply':
return a * b
elif operation == 'divide':
return a / b if b != 0 else "Cannot divide by zero"
else:
return "Invalid operation"

print(calculator(5, 3, 'add')) # 8

22. Write a Python code to check if a number is a perfect


square
def is_perfect_square(x):
return int(x**0.5)**2 == x

print(is_perfect_square(16)) # True
print(is_perfect_square(14)) # False

23. Write a Python code to find the GCD of two numbers


def gcd(a, b):
while b:
a, b = b, a % b
return a
print(gcd(48, 18)) # 6

24. Write a Python code to convert a list of temperatures


from Celsius to Fahrenheit
def celsius_to_fahrenheit(temps):
return [(temp * 9/5) + 32 for temp in temps]
print(celsius_to_fahrenheit([0, 20, 37])) # [32.0, 68.0, 98.6]

25. Write a Python code to implement a queue using


collections.deque
from collections import deque

class Queue:
def __init__(self):
self.queue = deque()

def enqueue(self, item):


self.queue.append(item)

def dequeue(self):
return self.queue.popleft() if self.queue else None

q = Queue()
q.enqueue(1)
q.enqueue(2)
print(q.dequeue()) # 1

Python Coding Interview Tips


Here are some tips to help succeed in the technical interview:
Engage in coding questions for practice to build your skills.
Focus on understanding rather than memorizing code.
Participate in competitive coding challenges to test yourself.
Analyze different approaches to solve the same problem.
Use online compilers to check your codes with some test cases.

Conclusion
In conclusion, understanding and practicing Python coding questions across various levels is crucial for people
who want to clear their interviews. Whether you’re a beginner, intermediate, or advanced developer, familiarity
with these questions can boost your confidence and help you secure your desired position.

Learn From Industry Ready


Experts and Transform Your
Career Today

You might also like