Professional Documents
Culture Documents
Google coding round test top 10 questions and Solution
Google coding round test top 10 questions and Solution
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:
--------------------------------------------------------------------------------------------------------------------------------------
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
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:
--------------------------------------------------------------------------------------------------------------------------------------
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
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:
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 = []
--------------------------------------------------------------------------------------------------------------------------------------
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
--------------------------------------------------------------------------------------------------------------------------------------
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 = []
--------------------------------------------------------------------------------------------------------------------------------------
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 = []
--------------------------------------------------------------------------------------------------------------------------------------
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 remove(self):
return heapq.heappop(self.heap)