Core Python Interview Practice (No Libraries)
Q1: Reverse a String
Problem: Reverse a string without using built-in functions.
s = "python"
reversed_s = ""
for char in s:
reversed_s = char + reversed_s
print(reversed_s)
Explanation: We build the reversed string by prepending each character.
Q2: Check if a Number is Prime
Problem: Check whether a number is a prime.
num = 29
is_prime = True
if num < 2:
is_prime = False
else:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
is_prime = False
break
print(is_prime)
Explanation: A prime number is divisible only by 1 and itself.
Q3: Count Vowels in a String
Problem: Count the number of vowels in a string.
s = "data engineer"
count = 0
for char in s:
if char.lower() in "aeiou":
count += 1
print(count)
Explanation: Loop through the string and count characters that are vowels.
Q4: Find Factorial
Problem: Calculate the factorial of a number using a loop.
Core Python Interview Practice (No Libraries)
n = 5
factorial = 1
for i in range(2, n+1):
factorial *= i
print(factorial)
Explanation: Multiply all integers from 1 to n.
Q5: Palindrome Check
Problem: Check if a string is a palindrome.
s = "radar"
is_palindrome = s == s[::-1]
print(is_palindrome)
Explanation: A palindrome reads the same forward and backward.
Q6: Sum of Digits
Problem: Find the sum of digits of a number.
num = 1234
total = 0
while num > 0:
total += num % 10
num //= 10
print(total)
Explanation: Use modulo and integer division to extract and sum digits.
Q7: Print Fibonacci Series
Problem: Print the first 10 Fibonacci numbers.
a, b = 0, 1
for _ in range(10):
print(a, end=" ")
a, b = b, a + b
Explanation: Start with 0 and 1; each number is the sum of the two before.
Q8: Find Maximum in List
Problem: Find the largest number in a list without using max().
nums = [4, 2, 9, 5, 6]
maximum = nums[0]
Core Python Interview Practice (No Libraries)
for num in nums:
if num > maximum:
maximum = num
print(maximum)
Explanation: Compare each number to track the maximum.
Q9: Count Frequency of Words
Problem: Count the frequency of each word in a sentence.
sentence = "this is a test this is"
words = sentence.split()
freq = {}
for word in words:
if word in freq:
freq[word] += 1
else:
freq[word] = 1
print(freq)
Explanation: Use a dictionary to track occurrences of each word.
Q10: Find Even Numbers in List
Problem: Print all even numbers from a list.
nums = [1, 2, 3, 4, 5, 6]
for num in nums:
if num % 2 == 0:
print(num)
Explanation: Use modulo to find even numbers.