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

Python Interview Problems

Uploaded by

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

Python Interview Problems

Uploaded by

ccode1942
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Basic Python Problem-Solving Questions with

Solutions

1. Reverse a String
s = input("Enter a string: ") print("Reversed:", s[::-1])

2. Check Palindrome
s = input("Enter a string: ").replace(" ", "").lower() print("Palindrome?", s ==
s[::-1])

3. Largest Element in a List


nums = list(map(int, input("Enter numbers separated by space: ").split()))
print("Largest:", max(nums))

4. Count Occurrences
nums = list(map(int, input("Enter numbers: ").split())) x = int(input("Enter element
to count: ")) print("Count:", nums.count(x))

5. Fibonacci Sequence
n = int(input("How many Fibonacci numbers? ")) a, b = 0, 1 for _ in range(n):
print(a, end=" ") a, b = b, a + b

6. Prime Number Check


n = int(input("Enter a number: ")) if n > 1 and all(n % i != 0 for i in range(2,
int(n**0.5)+1)): print("Prime") else: print("Not Prime")

7. Sum of Digits
n = int(input("Enter a number: ")) print("Sum of digits:", sum(int(d) for d in
str(n)))

8. Remove Duplicates
nums = list(map(int, input("Enter numbers: ").split())) print("Without duplicates:",
list(set(nums)))

9. Merge Two Sorted Lists


a = list(map(int, input("Enter sorted list A: ").split())) b = list(map(int,
input("Enter sorted list B: ").split())) i = j = 0 merged = [] while i < len(a) and
j < len(b): if a[i] < b[j]: merged.append(a[i]); i += 1 else: merged.append(b[j]); j
+= 1 merged.extend(a[i:]); merged.extend(b[j:]) print("Merged:", merged)
10. Common Elements
a = list(map(int, input("List A: ").split())) b = list(map(int, input("List B:
").split())) print("Common:", list(set(a) & set(b)))

11. Anagram Check


s1 = input("Enter first string: ") s2 = input("Enter second string: ")
print("Anagram?", sorted(s1) == sorted(s2))

12. First Non-Repeating Character


s = input("Enter a string: ") for c in s: if s.count(c) == 1: print("First
non-repeating:", c) break

13. All Subsets


from itertools import combinations nums = list(map(int, input("Enter numbers:
").split())) subsets = [] for r in range(len(nums)+1):
subsets.extend(combinations(nums, r)) print("Subsets:", subsets)

14. Missing Number


nums = list(map(int, input("Enter sequence (missing one): ").split())) n = len(nums)
+ 1 expected = n*(n+1)//2 print("Missing number:", expected - sum(nums))

15. Armstrong Number


n = int(input("Enter a number: ")) digits = str(n) power = len(digits) if
sum(int(d)**power for d in digits) == n: print("Armstrong") else: print("Not
Armstrong")

16. Stack Implementation


stack = [] while True: choice = input("Push/Pop/Exit: ").lower() if choice ==
"push": val = input("Enter value: ") stack.append(val) print("Stack:", stack) elif
choice == "pop": if stack: print("Popped:", stack.pop()) else: print("Empty stack")
else: break

17. Queue Implementation


queue = [] while True: choice = input("Enqueue/Dequeue/Exit: ").lower() if choice ==
"enqueue": val = input("Enter value: ") queue.append(val) print("Queue:", queue)
elif choice == "dequeue": if queue: print("Dequeued:", queue.pop(0)) else:
print("Empty queue") else: break

18. Largest Subarray Sum


nums = list(map(int, input("Enter numbers: ").split())) max_sum = curr_sum = nums[0]
for num in nums[1:]: curr_sum = max(num, curr_sum + num) max_sum = max(max_sum,
curr_sum) print("Largest subarray sum:", max_sum)

19. Sort Without Built-in


nums = list(map(int, input("Enter numbers: ").split())) for i in range(len(nums)):
for j in range(i+1, len(nums)): if nums[i] > nums[j]: nums[i], nums[j] = nums[j],
nums[i] print("Sorted:", nums)

20. Count Vowels


s = input("Enter a string: ") vowels = "aeiouAEIOU" print("Vowel count:", sum(1 for
c in s if c in vowels))

You might also like