0% found this document useful (0 votes)
47 views

Q2. Can Binary Search Be Used For Linked Lists?

An algorithm is a well-defined computational procedure that takes input and produces output. Algorithms are needed to improve efficiency, compare techniques, provide understanding of problems, and measure complexity. Binary search cannot be used for linked lists due to lack of random access. To find the k-th largest element in an array, use a min heap of the first k elements and compare subsequent elements to the heap root.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Q2. Can Binary Search Be Used For Linked Lists?

An algorithm is a well-defined computational procedure that takes input and produces output. Algorithms are needed to improve efficiency, compare techniques, provide understanding of problems, and measure complexity. Binary search cannot be used for linked lists due to lack of random access. To find the k-th largest element in an array, use a min heap of the first k elements and compare subsequent elements to the heap root.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Q1. What is an algorithm? What is the need for an algorithm?

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.

Q2. Can Binary Search be used for linked lists?


Ans: Since random access is not allowed in linked list, we cannot reach the middle element in
O(1) time. Therefore Binary Search is not possible for linked lists.
Q3. Given a big array, how to efficiently find k’th largest element in it?
Ans: There can be many solutions for this. The best solution is to use min heap. We Build a Min
Heap MH of the first k elements. For each element, after the kth element (arr[k] to arr[n-1]),
compare it with root of MH, if the element is greater than the root then make it root and call
heapify for MH, Else ignore it. Finally, MH has k largest elements and root of the MH is the kth
largest element.
Q4. What is the Complexity of Algorithm?
Ans: The complexity of the algorithm is a way to classify how efficient an algorithm is compared
to alternative ones. Its focus is on how execution time increases with the data set to be processed.
The computational complexity of the algorithm is important in computing.
Time complexity: Time complexity is a Running time of a program as a function of the size of
the input.
Space complexity: Space complexity analyzes the algorithms, based on how much space an
algorithm needs to complete its task. Space complexity analysis was critical in the early days of
computing (when storage space on the computer was limited).
Nowadays, the problem of space rarely occurs because space on the computer is broadly enough.
We achieve the following types of analysis for complexity
Worst-case f(n):It is defined by the maximum number of steps taken on any instance of size n.
Best-case f(n):It is defined by the minimum number of steps taken on any instance of size n.

1
Average-case f(n) :It is defined by the average number of steps taken on any instance of size n.

Q5. What is Divide and Conquer algorithms?


Ans: Divide and Conquer is not an algorithm; it's a pattern for the algorithm. It is designed in a
way as to take dispute on a huge input, break the input into minor pieces, and decide the problem
for each of the small pieces. Now merge all of the piecewise solutions into a global solution.
This strategy is called divide and conquer.
Divide and conquer uses the following steps to make a dispute on an algorithm.
Divide: In this section, the algorithm divides the original problem into a set of subproblems.
Conquer: In this section, the algorithm solves every subproblem individually.
Combine: In this section, the algorithm puts together the solutions of the subproblems to get the
solution to the whole problem
Q6: What is Dijkstra's shortest path algorithm?
Ans: Dijkstra's algorithm is an algorithm for finding the shortest path from a starting node to the
target node in a weighted graph. The algorithm makes a tree of shortest paths from the starting
vertex and source vertex to all other nodes in the graph.
Q7: Give some examples of Divide and Conquer algorithm?
Some problems that use Divide and conquer algorithm to find their solution are listed below.
o Merge Sort
o Quick Sort
o Binary Search
o Strassen's Matrix Multiplication
o Closest pair (points)
Q8. What are Greedy algorithms? Give some example of it?
Ans: A greedy algorithm is an algorithmic strategy which is made for the best optimal choice at
each sub stage with the goal of this, eventually leading to a globally optimum solution. This
means that the algorithm chooses the best solution at the moment without regard for
consequences.
In other words, an algorithm that always takes the best immediate, or local, solution while
finding an answer.
Below is a list of algorithms that finds their solution with the use of the Greedy algorithm.
o Travelling Salesman Problem
o Prim's Minimal Spanning Tree Algorithm
o Kruskal's Minimal Spanning Tree Algorithm
o Dijkstra's Minimal Spanning Tree Algorithm
o Graph - Map Coloring
o Graph - Vertex Cover
o Knapsack Problem
o Job Scheduling Problem
Q9. What is a linear search?
Ans: Linear search is used on a group of items. It relies on the technique of traversing a list from
start to end by visiting properties of all the elements that are found on the way.
Q10. How to delete a node in a given link list? Write an algorithm and a program?

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

Q23. Which Are The Advantages Provided By Insertion Sort?


Ans : Insertion sort provides several advantages:
a) simple implementation
b) efficient for small data sets
c) adaptive - efficient for data sets that are already substantially sorted
d) more efficient in practice than most other simple quadratic,
e) stable - does not change the relative order of elements with equal keys
f) in-place - only requires a constant amount O( 1) of additional memory space
g) online - can sort a list as it receives it

Q24. Which Are The Main Steps Of A Merge Sorting Algorithm?


Ans: Sorting by merging is a recursive, divide-and-conquer strategy. The basic steps to perform
are the following:
a) divide the sequence into two sequences of length
b) recursively sort each of the two subsequences
c) merge the sorted subsequences to obtain the final result

Q25. Find out the output of following table:


JobID Deadline Profit
a 2 100
b 1 19
c 2 27
d 1 25
e 3 15
Q26. Find out the code words for all following characters :

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?

Q33. Please practice the algo last assignment ques.

You might also like