Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Technical Question With Answer

Question:- 1. How would you reverse a given string?

Solution:-1 To reverse a given string, you can use the built-in reverse() method, or you can
loop through the string and build a new string using the characters in reverse order.

For example:

string = "Hello World!"


#Using the built-in reverse() method
reversed_string = string.reverse()
print(reversed_string) # output: !dlroW olleH

#Looping through the string


reversed_string = ""
for char in string:
reversed_string = char + reversed_string
print(reversed_string) # output: !dlroW olleH

--------------------------------------------------------------------------------------------------------------------------------------

Question 2. How would you find the maximum value in an array?

Solution:- 2
To find the maximum value in an array, you can loop through the array and compare
each element to the current maximum value. If the current
element is greater than the current maximum value, it becomes the new maximum
value.
For example:

array = [4, 7, 2, 9, 3]
max_value = array[0]

for i in range(1,len(array)):
if array[i] > max_value:
max_value = array[i]

print
(max_value) # output: 9

Question 3. How would you implement a binary search tree?

Solution 3. To implement a binary search tree, you first need to create a class to represent a node in
the tree. The node should have fields for the data it stores, and references to its left and
right children. You then need to create methods to insert and search for data in the tree.

For example:-

class Node:

def __init__(self, data):


self.data = data
self.left = None
self.right = None

def insert(root, data):


if root is None:
root = Node(data)
elif data < root.data:
root.left = insert(root.left, data)
else:
root.right = insert(root.right, data)
return root

def search(root, data):


if root is None or root.data == data:
return root
elif data < root.data:
return search(root.left, data)
else:
return search(root.right, data)

--------------------------------------------------------------------------------------------------------------------------------------

Question 4. How would you check if a given linked list is circular?

Solution 4. To check if a given linked list is circular, you can use a slow and fast pointer approach.
You start with two pointers, one pointing to the head of the list and another pointing to the
second node. Then, you move the slow pointer one node at a time, and the fast pointer two
nodes at a time. If the two pointers ever meet, then the list is circular.

For example:

# Node class
class Node:
def __init__(self, data):
self.data = data
self.next = None
# LinkedList class
class LinkedList:
def __init__(self):
self.head = None

def is_circular(linked_list):
# start with two pointers
slow_p = linked_list.head
fast_p = linked_list.head

while(slow_p and fast_p and fast_p.next):


slow_p = slow_p.next
fast_p = fast_p.next.next
if slow_p == fast_p:
return True

return False
Question 5. How would you implement a stack using an array?

Solution 5. To implement a stack using an array, you can create an array that is initialized to a
certainsize. You can then use two variables to keep track of the top of the stack. You can
have a "top" variable that stores the index of the top element in the stack, and a "size"
variable that stores the current size of the stack. To push an element onto the stack, you add
the element to the array at the index of the "top" variable. To pop an element from the
stack, you remove the element from the array at the index of the "top" variable and
decrement the "top" variable.
For example:

# Initialize array size


N = 10
# Create array
stack = [None] * N
# Set top to -1
top = -1
# Set size to 0
size = 0
# Push an element onto the stack
def push(element):
global top
global size
# Check if stack is full
if size == N:
print("Stack is full")
return
top += 1
stack[top] = element
size += 1

# Pop an element from the stack


def pop():
global top
global size
# Check if stack is empty
if size == 0:
print("Stack is empty")
return
element = stack[top]
top -= 1
size -= 1
return element
--------------------------------------------------------------------------------------------------------------------------------------
Question 6. How would you design a class to represent a graph structure?

Solution 6. To design a class to represent a graph structure, you need to create a class to represent a
node in the graph, and another class to represent the graph itself. The node class should
have fields for the data it stores, and references to its adjacent nodes. The graph class
should have a list of all the nodes in the graph, and methods to add and remove nodes from
the graph.

For example:

# Node class
class Node:
def __init__(self, data):
self.data = data
self.adjacent = []

# Graph class
class Graph:
def __init__(self):
self.nodes = []

def add_node(self, data):


node = Node(data)
self.nodes.append(node)

def remove_node(self, data):


for i, node in enumerate(self.nodes):
if node.data == data:
del self.nodes[i]
break

--------------------------------------------------------------------------------------------------------------------------------------
Question 7. How would you implement a hash table data structure?

Solution 7. To implement a hash table data structure, you first need to create a class to represent
the hash table. The hash table should have an array of buckets, and each bucket should be a
linked list of key-value pairs. You then need to create methods to add and search for data in
the hash table.

For example:

# Node class
class Node:
def __init__(self, key, value):
self.key = key

self.value = value
self.next = None

# HashTable class
class HashTable:
def __init__(self, size):
self.buckets = [None] * size

def add(self, key, value):


index = self._hash(key)
node = self.buckets[index]
if node is None:
self.buckets[index] = Node(key, value)
return
prev = node
while node is not None:
prev = node
node = node.next
prev.next = Node(key, value)

def search(self, key):


index = self._hash(key)
node = self.buckets[index]
while node is not None and node.key != key:
node = node.next
if node is None:
return None
return node.value

--------------------------------------------------------------------------------------------------------------------------------------
Question 8. How would you find the shortest path between two given nodes in a graph?

Solution 8. To find the shortest path between two given nodes in a graph, you can use a breadth-firs
search algorithm. The algorithm starts at the source node, and explores all its adjacent
nodes. Then, it explores all the nodes that are one edge away from the source node, and so
on. Eventually, it finds the shortest path from the source node to the destination node.

For example:

# Node class
class Node:
def __init__(self, data):
self.data = data
self.visited = False

self.adjacent = []

# Graph class
class Graph:
def __init__(self):
self.nodes = []

def find_shortest_path(self, source, dest):


# Create a queue for BFS
queue = []
# Mark the source node as visited and enqueue it
source.visited = True
queue.append(source)
# Loop until queue is empty
while queue:
# Dequeue a vertex from queue
node = queue.pop(0)
# If the dequeued vertex is the destination node, we are done
If node == dest:
break
# Get all adjacent nodes of the dequeued vertex
# and mark them as visited
for adjacent in node.adjacent:
if not adjacent.visited:
adjacent.visited = True
queue.append(adjacent)

--------------------------------------------------------------------------------------------------------------------------------------
Question 9. How would you detect cycles in a directed graph?

Solution 9. To detect cycles in a directed graph, you can use a depth-first search algorithm. The

algorithm starts at a given node, and explores all its adjacent nodes. Then, it explores all the
nodes that are one edge away from the starting node, and so on. If at any point the
algorithm visits a node that it has already visited, then there is a cycle in the graph.

For example:

# Node class
class Node:
def __init__(self, data):
self.data = data
self.visited = False
self.adjacent = []

# Graph class
class Graph:
def __init__(self):
self.nodes = []

def detect_cycle(self, node):


# Mark the current node as visited
node.visited = True
# Recur for all the nodes adjacent to this node
for adjacent in node.adjacent:
if not adjacent.visited:
if self.detect_cycle(adjacent):
return True
elif adjacent.visited:
return True
return False

--------------------------------------------------------------------------------------------------------------------------------------
Question 10. How would you implement a priority queue using a heap?

Solutino 10. To implement a priority queue using a heap, you can create a class to represent the
priority queue. The priority queue should have a heap to store the elements, and methods
to add and remove elements from the queue. To add an element to the queue, you add the
element to the heap. To remove an element from the queue, you remove the root element
from the heap.

For example:

# PriorityQueue class
class PriorityQueue:
def __init__(self):
self.heap = []

def add(self, element):


heapq.heappush(self.heap, element)

def remove(self):
return heapq.heappop(self.heap)

You might also like