Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Python Coding Interview Questions and Answers

Ques 1. Write a function to reverse a string.

def reverse_string(input_str):

return input_str[::-1]

Example:

reverse_string('hello')  # Output: 'olleh'

Is it helpful? Add Comment View Comments
 

Ques 2. Implement a function to check if a string is a palindrome.

def is_palindrome(input_str):

return input_str == input_str[::-1]

Example:

is_palindrome('radar')  # Output: True

Is it helpful? Add Comment View Comments
 

Ques 3. Write a Python program to find the factorial of a number.

def factorial(n):

  return 1 if n == 0 else n * factorial(n-1)

Example:

factorial(5)  # Output: 120

Is it helpful? Add Comment View Comments
 

Ques 4. Implement a function to check if a number is prime.

def is_prime(num):

  if num < 2:

  return False

  for i in range(2, int(num**0.5) + 1):

    if num % i == 0:

      return False

   return True

Example:

is_prime(11)  # Output: True

Is it helpful? Add Comment View Comments
 

Ques 5. Create a Python class for a basic stack implementation.

class Stack:

  def __init__(self):

    self.items = []

 

  def push(self, item):

    self.items.append(item)

 

  def pop(self):

    return self.items.pop()

 

  def is_empty(self):

    return len(self.items) == 0

Example:

stack = Stack()
stack.push(1)
stack.push(2)
print(stack.pop())  # Output: 2

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2024 WithoutBook