Q2. Can Binary Search Be Used For Linked Lists?
Q2. Can Binary Search Be Used For Linked Lists?
Ans: Informally, an algorithm is any well-defined computational procedure that takes some
value, or set of values, as input and produces some value, or set of values, as output. An
algorithm is thus a sequence of computational steps that transform the input into the output.
Need for Algorithm
The algorithm provides the basic idea of the problem and an approach to solve it. Some reasons
to use an algorithm are as follows.
o The algorithm improves the efficiency of an existing technique.
o To compare the performance of the algorithm with respect to other techniques.
o The algorithm gives a strong description of requirements and goal of the problems to the
designer.
o The algorithm provides a reasonable understanding of the flow of the program.
o The algorithm measures the performance of the methods in different cases (Best cases,
worst cases, average cases).
o The algorithm identifies the resources (input/output, memory) cycles required by the
algorithm.
o With the help of an algorithm, we can measure and analyze the complexity time and
space of the problems.
o The algorithm also reduces the cost of design.
1
Average-case f(n) :It is defined by the average number of steps taken on any instance of size n.
2
Ans: Write a function to delete a given node from a Singly Linked List. The function must
follow the following constraints:
o The function must accept a pointer to the start node as the first argument and node to be
deleted as the second argument, i.e., a pointer to head node is not global.
o The function should not return a pointer to the head node.
o The function should not accept pointer to pointer to head node.
Q11. Write a c program to merge a link list into another at an alternate position?
We have two linked lists; insert nodes of the second list into the first list at substitute positions of
the first list.
Example
if first list is 1->2->3 and second is 12->10->2->4->6, the first list should become 1->12->2->10-
>17->3->2->4->6 and second list should become empty. The nodes of the second list should
only be inserted when there are positions available.
Q12. What is the difference between the Singly Linked List and Doubly Linked List data
structure?
Ans: You cannot traverse back in a singly linked list because in it a node only points towards the
next node and there is no pointer to the previous node.
On the other hand, the doubly linked list allows you to navigate in both directions in any linked
list because it maintains two pointers towards the next and previous node.
Q13. Mention what are the types of Notation used for Time Complexity?
Ans: Big Oh: It indicates "fewer than or the same as" <expression>iterations
Big Omega: It indicates "more than or same as" <expression>iterations
Big Theta: It indicates "the same as"<expression>iterations
Little Oh: It indicates "fewer than" <expression>iterations
Little Omega: It indicates "more than" <expression>iterations
Q14. Explain what a "Hash Algorithm" is and what are they used for?
Ans: "Hash Algorithm" is a hash function that takes a string of any length and decreases it to a
unique fixed length string. It is used for password validity, message & data integrity and for
many other cryptographic systems.
Q15: Explain what is a recursive algorithm?
Ans: Recursive algorithm is a method of solving a complicated problem by breaking a problem
down into smaller and smaller sub-problems until you get the problem small enough that it can
be solved easily. Usually, it involves a function calling itself.
Q16. Mention what are the three laws of recursion algorithm?
All recursive algorithm must follow three laws
It should have a base case
A recursive algorithm must call itself
A recursive algorithm must change its state and move towards the base case
Q17. Explain what is bubble sort algorithm?
Ans: Bubble sort algorithm is also referred as sinking sort. In this type of sorting, the list to be
sorted out compares the pair of adjacent items. If they are organized in the wrong order, it will
swap the values and arrange them in the correct order.
Q18. How To Find Median Of A BST?
Ans : Find the no. of elements on the left side.
o If it is n-1 the root is the median.
3
o If it is more than n-1, then it has already been found in the left subtree.
o Else it should be in the right subtree
Q19. What Is The Goal Of The Shortest Distance Algorithm?
Ans :The goal is completely fill the distance array so that for each vertex v, the value of
distance[v] is the weight of the shortest path from start to v.
Q20. What Is A Backtracking Algorithm? Provide Several Examples?
Ans :It is an algorithm that considers systematically all possible outcomes for each decision.
Examples of backtracking algorithms are the eight queens problem or generating permutations of
a given sequence.
Q21. What Is A Greedy Algorithm? Give Examples Of Problems Solved Using Greedy
Algorithms?
Ans : A greedy algorithm is any algorithm that makes the local optimal choice at each stage with
the hope of finding the global optimum. A classical problem which can be solved using a greedy
strategy is the traveling salesman problem. Another problems that can be solved using greedy
algorithms are the graph coloring problem and all the NP-complete problems.
Q22. Describe On Short An Insertion Sorting Algorithm.?
Ans : An algorithm that sorts by insertion takes the initial, unsorted sequence and computes a
series of sorted sequences using the following rules:
a) the first sequence in the series is the empty sequence
b) given a sequence S(i) in the series, for 0<=i
4
Q27. How many edges does a minimum spanning tree has?
Ans: A minimum spanning tree has (V – 1) edges where V is the number of vertices in the given
graph.
Q28. How does MST work by using Kruskal’s algorithm
1. Sort all the edges in non-decreasing order of their weight.
2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle
is not formed, include this edge. Else, discard it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.
Q29. Given an Array of integers, find maximum sum subarray among all subarray
possible.
A [] = [2, -4, 1, 9, -5, 7, 3]
Ans: The maximum sum of subarray is 12.
Q30. What is the difference among a Subarray/substring, subsequence, and subset?
Ans :
Q31. Write the sequence of the following tree by using inorder traversing.
5
Q32. Consider the graph shown below. What are the edges in the MST of the given graph?