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

Python Interview Coding Questions-Ad

The document contains a series of Python coding interview questions with corresponding solutions. It covers topics such as string manipulation, prime number checking, factorial calculation, even/odd determination, vowel counting, list deduplication, common elements in lists, dictionary merging, list sorting, and character counting. Each question is accompanied by code snippets demonstrating the implementation of the solutions.

Uploaded by

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

Python Interview Coding Questions-Ad

The document contains a series of Python coding interview questions with corresponding solutions. It covers topics such as string manipulation, prime number checking, factorial calculation, even/odd determination, vowel counting, list deduplication, common elements in lists, dictionary merging, list sorting, and character counting. Each question is accompanied by code snippets demonstrating the implementation of the solutions.

Uploaded by

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

***PYTHON INTERVIEW CODING QUESTIONS***

------------------------------------------------------------------------------------------------------------------------------------------

1.Write a Python function to reverse a string?

A .Python function to reverse a String :

def reverse_string(s):

return s[::-1]

print(reverse_string("hello")) # Output: "olleh"

------------------------------------------------------------------------------------------------------------------------------------------

2. Write a Python function to check if a number is prime?


A. Python function to check if a number is prime or not :

Solution -1:

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

print(is_prime(7)) # Output: True

Solution-2:

num = int(input(“Enter a number:”))

# To take input from the user

#num = int(input("Enter a number: "))

if num == 0 or num == 1:

print(num, "is not a prime number")


elif num > 1:

# check for factors

for i in range(2,num):

if (num % i) == 0:

print(num,"is not a prime number")

print(i,"times",num//i,"is",num)

break

else:

print(num,"is a prime number")

# if input number is less than

# or equal to 1, it is not prime

else:

print(num,"is not a prime number")

Solution-3 :

# Input number from the user

num = int(input("Enter a number: "))

# A prime number is greater than 1 and divisible only by 1 and itself

if num > 1:

# Check divisibility from 2 to num-1

for i in range(2, num):

if num % i == 0:

print(num, "is not a prime number")

break
else:

print(num, "is a prime number")

else:

print(num, "is not a prime number")

------------------------------------------------------------------------------------------------------------------------------------------

3. Write a Python function to find the factorial of a number.

A. Python function to find the factorial of a number :


def fact(n):

If n==1 or n==0:

return 1

return n*fact(n-1)

n=int(input(“Enter a number:”))

print(fact(n))

------------------------------------------------------------------------------------------------------------------------------------------

4. Write a Python program to check if a number is even or odd.


A. Python program to check the given number is even or odd :

num=int(input(“enter a number :”))


if num%2==0:
print(“Given number”,num,”is even”)
else:
print(“Given number”,num,”is odd”)
------------------------------------------------------------------------------------------------------------
5. Write a program to count the number of vowels in a given string .

A.

# Input string from the user

string = input("Enter a string: ")

# Define the vowels


vowels = "aeiouAEIOU"

# Initialize the count to 0

count = 0

# Loop through each character in the string

for char in string:

if char in vowels:

count += 1

# Print the result

print("Number of vowels in the string:", count)

------------------------------------------------------------------------------------------------------------------------------------
6. How do you remove duplicates from a list?
A.

my_list = [1, 2, 2, 3, 4, 4, 5]

unique_list = list(set(my_list))

print(unique_list) # Output: [1, 2, 3, 4, 5] (Order may vary)

(OR)

my_list = [1, 2, 2, 3, 4, 4, 5]

unique_list = []

[unique_list.append(x) for x in my_list if x not in unique_list]

print(unique_list) # Output: [1, 2, 3, 4, 5]

------------------------------------------------------------------------------------------------------------------------------------------
7. Write a program to find the common elements in two lists.
A.

def common_elements(list1, list2):

return list(set(list1) & set(list2))


# Example usage

list1 = [1, 2, 3, 4, 5]

list2 = [4, 5, 6, 7, 8]

print(common_elements(list1, list2)) # Output: [4, 5]

(OR)

def common_elements(list1, list2):

return [x for x in list1 if x in list2]

# Example usage

list1 = [1, 2, 3, 4, 5]

list2 = [4, 5, 6, 7, 8]

print(common_elements(list1, list2)) # Output: [4, 5]

------------------------------------------------------------------------------------------------------------------------------------------

8. How do you merge two dictionaries in Python?


A.

dict1 = {'a': 1, 'b': 2}

dict2 = {'b': 3, 'c': 4}

merged_dict = dict1 | dict2

print(merged_dict) # Output: {'a': 1, 'b': 3, 'c': 4}

(OR)

dict1 = {'a': 1, 'b': 2}

dict2 = {'b': 3, 'c': 4}

dict1.update(dict2)

print(dict1) # Output: {'a': 1, 'b': 3, 'c': 4}

------------------------------------------------------------------------------------------------------------------------------------------
9. Write a program to sort a list of numbers.
A.

def sort_numbers(numbers):

return sorted(numbers)

------------------------------------------------------------------------------------------------------------------------------------------

10. How can you count the occurrences of each character in a string?
A.

Using collections.Counter (Best for Simplicity & Performance)

from collections import Counter

text = "hello world"


char_count = Counter(text)

print(char_count) # Output: Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})

Using a Dictionary (Manual Counting)


text = "hello world"
char_count = {}

for char in text:


char_count[char] = char_count.get(char, 0) + 1

print(char_count) # Output: {'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1}
------------------------------------------------------------------------------------------------------------------------------------------

You might also like