0% found this document useful (0 votes)
2 views14 pages

Python Coding Questions

The document contains a list of Python coding questions commonly asked in GenC interviews, along with sample inputs and outputs for each problem. It includes various programming challenges such as reversing a string, checking for prime numbers, counting vowels, and finding the factorial of a number. Each question is accompanied by a simple code implementation to solve the problem.

Uploaded by

devaraj72003
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

Python Coding Questions

The document contains a list of Python coding questions commonly asked in GenC interviews, along with sample inputs and outputs for each problem. It includes various programming challenges such as reversing a string, checking for prime numbers, counting vowels, and finding the factorial of a number. Each question is accompanied by a simple code implementation to solve the problem.

Uploaded by

devaraj72003
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PYTHON CODING QUESTIONS (Most Asked in GenC Interviews)

1. Reverse a string without using slicing

Input: "hello"
Output: "olleh"

PRG: str =input()

rev=""

for char in str:

rev=char+rev

print(rev)

2. Check if a number is prime

Input: 7
Output: "Prime"

PRG: num=int(input())

if num<=0:

print("number is not prime")

else:

is_prime=True;

for i in range(2,num):

if num%i==0:

is_prime=False

break

if is_prime:

print("prime")

else:

print("not prime")

3. Find the factorial of a number (without recursion)

Input: 5
Output: 120
PRG: num=int(input())

fact = 1

for i in range(1,num):

fact=fact*num

num=num-1

print(fact)

4. Count vowels in a string

Input: "education"
Output: 5

PRG: str=input()

count = 0

for char in str:

if (char=='a'or char=='e'or char=='i' or char=='o' or char=='u'):

count=count+1

print(count)

5. Find largest and smallest number in a list

Input: [10, 2, 5, 8]
Output: 10 2

PRG: num=[10,2,5,8]

largest=num[0]

smallest=num[0]

for i in num:

if i > largest:

largest=i

for n in num:

if n< smallest:
smallest=n

print(largest)

print(smallest)

6. Sum of digits of a number

Input: 1234
Output: 10

PRG: n=input()

sum=int(n[0])+int(n[1])+int(n[2])+int(n[3])

print(sum)

7. Print Fibonacci series up to N terms

Input: 7
Output: 0 1 1 2 3 5 8

PRG: n=int(input())

a=0

b=1

for i in range(n):

print(a,end="")

c=a+b

a=b

b=c

8. Check if a string is palindrome (without built-in functions)

Input: "level"
Output: True

PRG: str=input()

rev=""

for char in str:

rev=char+rev

if(str==rev):

print("Palindrome")
else:

print("Not Palindrome")

9. Count frequency of each character in a string

Input: "aabbc"
Output: a:2 b:2 c:1

PRG: s = input()

count = {}

for char in s:

if char in count:

count[char] += 1

else:

count[char] = 1

for char, freq in [Link]():

print(char, ":", freq)

10. Remove duplicates from a list without using set()

Input: [1,2,2,3,3,4]
Output: [1,2,3,4]

PRG: num=[1,2,2,3,3,4]

duplicates=[]

for i in num:

if i not in duplicates:

[Link](i)

print(duplicates)

11. Find second largest number in a list

Input: [10, 40, 20, 30]


Output: 30

PRG: num=[10,40,20,30]

largest=num[0]

for i in num:

if i > largest:
largest=i

second_largest=num[0]

for i in num:

if i!=largest and i>second_largest:

second_largest=i

print(second_largest)

12. Check Armstrong number

Input: 153
Output: Armstrong

PRG: num=int(input())

temp=num

sum=0

while temp > 0:

digit=temp%10

sum = sum + digit**3

temp=temp//10

if sum==num:

print("Armstrong")

else:

print("Not Armstrong")

13. Program to merge two dictionaries

Input: {a:1, b:2}, {c:3}


Output: {a:1,b:2,c:3}

PRG: dict1 = {"a":1,"b":2}

dict2 = {"c":3}

[Link](dict2)

print(dict1)

14. Count even & odd numbers in a list

Input: [1,2,3,4,5]
Output: Even:2 Odd:3
PRG: num=[1,2,3,4]

even_count = 0

odd_count = 0

for i in num:

if i%2==0:

even_count = even_count + 1

else:

odd_count=odd_count+1

print("odd:",odd_count)

print("even:",even_count)

15. Find the missing number in a sequence

Input: [1,2,4,5]
Output: 3

PRG: num=[1,2,4,5]

for i in range(1,len(num)+1):

if i not in num:

print(i)

break

16. Check if two strings are anagrams

Input: listen, silent


Output: True

PRG: str1=input()

str2=input()

if sorted(str1)== sorted(str2):

print("True")

else:

print("False")

17. Find GCD of two numbers

Input: 12, 18
Output: 6
PRG: a = int(input("Enter first number: "))

b = int(input("Enter second number: "))

while b != 0:

a, b = b, a % b

print("GCD:", a)

18. Find LCM of two numbers

PRG: a = int(input("Enter first number: "))

b = int(input("Enter second number: "))

# Step 1: Find GCD using Euclidean algorithm

x, y = a, b

while y != 0:

x, y = y, x % y

gcd = x

# Step 2: Use formula LCM = (a*b) // GCD

lcm = (a * b) // gcd

print("LCM:", lcm)

19. Convert a list into a dictionary with index as key

Input: ['a','b','c']
Output: {0:'a',1:'b',2:'c'}

PRG: lst = ['a', 'b', 'c']

result = {}

for index in range(len(lst)):

result[index] = lst[index]

print(result)

20. Print numbers divisible by 3 but not 5 from 1 to N

PRG : num=int(input())

for i in range(1,num+1):

if i%3==0 and i%5!=0:

print(i)
21. Replace all occurrences of a character in a string

Input: "banana", replace 'a' with 'x'


Output: "bxnxnx"

PRG: str=input()

out=""

for ch in str:

if ch== 'a':

out=out+"x"

else:

out=out+ch

print(out)

22. Find the sum of even numbers in a list

PRG : num=[1,2,3,4]

sum=0

for i in num:

if i%2==0:

sum=sum+i

print(sum)

23. Check if a number is perfect

Perfect number example: 6, 28

PRG: num=int(input())

sum=0

for i in range(1,num):

if num%i==0:

sum=sum+i

if(num==sum):

print("perfect")

else:

print("not perfect")
24. Merge two sorted lists without using sort()

PRG:

25. Print pattern

**

***

****

PRG: for i in range(1,5):

print()

for j in range(1,1+i):

print("*",end="")

26. Count words in a sentence

Input: "Python is easy"


Output: 3

PRG: str=input()

words=[Link]()

print(len(words))

27. Find common elements between two lists

PRG: l1=[1,2,3,4]

l2=[4,5,6,7]

dupi=[]

for i in l1:

if i in l2:

dupi=i

print(dupi)

28. Remove all vowels from a string

PRG: str=input()

rem=""

for ch in str:

if ch not in "aeiou":
rem=rem+ch

print(rem)

29. Swap two numbers without using a temporary variable

PRG: a=int(input())

b=int(input())

a=a+b

b=a-b

a=a-b

print(a,b)

30. Get unique characters from a string

Input: "programming"
Output: "progamin"

PRG: str=input()

unique=""

for ch in str:

if ch not in unique:

unique=unique+ch

print(unique)

31. Find intersection of two sets

PRG: str={1,2,3,4}

str1={4,6,3,8}

result=[Link](str1)

print(result)

PRG1: set1 = {1, 2, 3, 4}

set2 = {3, 4, 5, 6}

common = set()

for i in set1:

if i in set2:

[Link](i)
print(common)

32. Convert binary to decimal

PRG:

33. Convert decimal to binary (without bin()).

PRG:

34. Check if a number is strong number

Strong number example: 145

PRG: num=int(input())

temp=num

act=0

while temp>0:

digit=temp%10

fact=1

for i in range(1,digit+1):

fact=fact*i

act=act+fact

temp//=10

if act == num:

print("Strong Number")

else:

print("Not a Strong Number")

35. Reverse each word in a string

Input: "hello world"


Output: "olleh dlrow"

PRG: str=input()

words=[Link]()

result=""
for w in words:

result=result+w[::-1]+""

print([Link]())

36. Find product of digits of a number

PRG: num=input()

product=1

for i in num:

product=product*int(i)

print(product)

37. Sum of squares of first N natural numbers

PRG: num=int(input())

sum=0

for i in range(1,num+1):

sq=i*i

sum=sum+sq

print(sum)

38. Print all prime numbers in a range

PRG: start= int(input("Enter start: "))

end = int(input("Enter end: "))

for num in range(start, end+1):

if num > 1:

is_prime = True

for i in range(2, num):

if num % i == 0:

is_prime = False

break

if is_prime:

print(num)

39. Sort a list manually using bubble sort

PRG: arr = [5, 2, 8, 1, 3]


n = len(arr)

for i in range(n-1):

for j in range(0, n-1-i):

if arr[j] > arr[j+1]:

arr[j], arr[j+1] = arr[j+1], arr[j]

print(arr)

40. Count uppercase, lowercase, digits in a string

PRG: str=input()

u_count=0

l_count=0

d_count=0

for ch in str:

If [Link]():

u_count=u_count+1

elif [Link]():

l_count=l_count+1

else:

d_count=d_count+1

print(u_count)

print(l_count)

print(d_count)

41. Check if list is sorted

Return True/False

PRG: list=[3,2,5,4]

is_sorted=True

for i in range(len(list)-1):

if list[i]>list[i+1]:

is_sorted=False

break
print(is_sorted)

with sorted fn :

lst = [3, 2, 5, 4]

if lst == sorted(lst):

print(True)

else:

print(False)

42. Rotate a list by k steps

PRG:

43. Find the longest word in a sentence

PRG: sentence = input("Enter a sentence: ")

words = [Link]() # split into words

longest = words[0] # assume first word is longest

for w in words:

if len(w) > len(longest):

longest = w

print("Longest word:", longest)

44. Convert Celsius to Fahrenheit

PRG: celsius = float(input("Enter temperature in Celsius: "))

fahrenheit = (celsius * 9/5) + 32

print("Temperature in Fahrenheit:", fahrenheit)

45. Check if substring exists in string (no in keyword)

PRG:

You might also like