Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Python Coding Interview Questions and Answers

Ques 6. Write a Python program to find the nth Fibonacci number.

def fibonacci(n):

  if n <= 1:

    return n

  else:

    return fibonacci(n-1) + fibonacci(n-2)

Example:

fibonacci(5)  # Output: 5

Is it helpful? Add Comment View Comments
 

Ques 7. Implement a function to find the intersection of two lists.

def intersection(list1, list2):

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

Example:

intersection([1, 2, 3], [2, 3, 4])  # Output: [2, 3]

Is it helpful? Add Comment View Comments
 

Ques 8. Write a Python program to perform matrix multiplication.

import numpy as np

matrix1 = np.array([[1, 2], [3, 4]])

matrix2 = np.array([[5, 6], [7, 8]])

result = np.dot(matrix1, matrix2)

Example:

print(result)
# Output: [[19, 22], [43, 50]]

Is it helpful? Add Comment View Comments
 

Ques 9. Create a generator function to generate Fibonacci numbers.

def fibonacci_generator():

  a, b = 0, 1

  while True:

    yield a

    a, b = b, a + b

Example:

fib_gen = fibonacci_generator()
print(next(fib_gen))  # Output: 0

Is it helpful? Add Comment View Comments
 

Ques 10. Implement a depth-first search (DFS) algorithm for a graph.

def dfs(graph, node, visited):

  if node not in visited:

    print(node)

    visited.add(node)

    for neighbor in graph[node]:

      dfs(graph, neighbor, visited)

Example:

graph = {1: [2, 3], 2: [4, 5], 3: [6], 4: [], 5: [], 6: []}
visited_set = set()
dfs(graph, 1, visited_set)

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2025 WithoutBook