Algorithm Questions and Answers

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

Algorithm Interview Questions and Answers

An algorithm is an integral part of any process so that interviewers will ask


you many questions related to the algorithm.

Here is the list of some most asked algorithm interview questions and
their answer. These questions are also beneficial for academic and
competitive exams perspective.

1) What is an algorithm? What is the need for an algorithm?


An algorithm is a well-defined computational procedure that takes some
values or the set of values, as an input and produces a set of values or some
values, as an 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.

2) What is the Complexity of Algorithm?


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.

It is very suitable to classify algorithm based on the relative amount of time


or relative amount of space they required and specify the growth of time/
space requirement as a function of input size.

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.

Average-case: f(n)

It is defined by the average number of steps taken on any instance of size n.

3) Write an algorithm to reverse a string. For example, if my string


is "uhsnamiH" then my result will be "Himanshu".
Algorithm to reverse a string.

Step1: start

Step2: Take two variable i and j

Step3: do length (string)-1, to set J at last position

Step4: do string [0], to set i on the first character.

Step5: string [i] is interchanged with string[j]

Step6: Increment i by 1

Step7: Increment j by 1

Step8: if i>j then go to step3

Step9: Stop

4) Write an algorithm to insert a node in a sorted linked list.


Algorithm to insert a node in a sorted linked list.

Case1:

Check if the linked list is empty then set the node as head and return it.

1. New_node-> Next= head;


2. Head=New_node
Case2:

Insert the new node in middle

1. While( P!= insert position)


2. {
3. P= p-> Next;
4. }
5. Store_next=p->Next;
6. P->Next= New_node;
7. New_Node->Next = Store_next;

Case3:

Insert a node at the end

1. While (P->next!= null)


2. {
3. P= P->Next;
4. }
5. P->Next = New_Node;
6. New_Node->Next = null;

5) What are the Asymptotic Notations?


Asymptotic analysis is used to measure the efficiency of an algorithm that
doesn't depend on machine-specific constants and prevents the algorithm
from comparing the time taking algorithm. Asymptotic notation is a
mathematical tool that is used to represent the time complexity of
algorithms for asymptotic analysis.

The three most used asymptotic notation is as follows.

θ Notation

θ Notation defines the exact asymptotic behavior. To define a behavior, it


bounds functions from above and below. A convenient way to get Theta
notation of an expression is to drop low order terms and ignore leading
constants.
Big O Notation

The Big O notation bounds a function from above, it defines an upper bound
of an algorithm. Let's consider the case of insertion sort; it takes linear time
in the best case and quadratic time in the worst case. The time complexity of
insertion sort is O(n2). It is useful when we only have upper bound on time
complexity of an algorithm.
Ω Notation

Just like Big O notation provides an asymptotic upper bound, the Ω


Notation provides an asymptotic lower bound on a function. It is useful
when we have lower bound on time complexity of an algorithm.
6) Explain the Bubble sort algorithm?
Bubble sort is the simplest sorting algorithm among all sorting algorithm. It
repeatedly works by swapping the adjacent elements if they are in the wrong
order.

e.g.

(72538) we have this array for sorting.

Pass1:

(72538) -> (27538) swap 7 and 2.


(27538) -> (25738) swap 7 and 5.
(25738) -> (25378) swap 7 and 3.
(25378) -> (25378) algorithm does not swap 7 and 8 because 7<8.

Pass2:

(25378) -> (25378) algorithm does not swap 2 and 5 because 2<5.
(25378) -> (23578) swap 3 and 5.
(23578) -> (23578) algorithm does not swap 5 and 7 because 5<7.
(23578) -> (23578) algorithm does not swap 7 and 8 because 7<8.

Here, the sorted element is (23578).

7) How to swap two numbers without swapping the temporary


variable in C?
1. #include <stdio.h>
2. int main() {
3. double a, b;
4. printf("Enter a: ");
5. scanf("%lf", &a);
6. printf("Enter b: ");
7. scanf("%lf", &b);

8. // swapping

9. // a = (initial_a - initial_b)
10. a = a - b;

11. // b = (initial_a - initial_b) + initial_b = initial_a


12. b = a + b;

13. // a = initial_a - (initial_a - initial_b) = initial_b


14. a = b - a;

15. // %.2lf displays numbers up to 2 decimal places


16. printf("After swapping, a = %.2lf\n", a);
17. printf("After swapping, b = %.2lf", b);

18. return 0;
19. }

Output
Enter a: 10.25
Enter b: -12.5
After swapping, a = -12.50
After swapping, b = 10.25

8) What is a Hash Table? How can we use this structure to find all
anagrams in a dictionary?
A Hash table is a data structure for storing values to keys of arbitrary type.
The Hash table consists of an index into an array by using a Hash function.
Indexes are used to store the elements. We assign each possible element to
a bucket by using a hash function. Multiple keys can be assigned to the same
bucket, so all the key and value pairs are stored in lists within their
respective buckets. Right hashing function has a great impact on
performance.

To find all anagrams in a dictionary, we have to group all words that contain
the same set of letters in them. So, if we map words to strings representing
their sorted letters, then we could group words into lists by using their sorted
letters as a key.

1. FUNCTION find_anagrams(words)
2. word_groups = HashTable<String, List>
3. FOR word IN words
4. word_groups.get_or_default(sort(word), []).push(word)
5. END FOR
6. anagrams = List
7. FOR key, value IN word_groups
8. anagrams.push(value)
9. END FOR
10. RETURN anagrams

The hash table contains lists mapped to strings. For each word, we add it to
the list at the suitable key, or create a new list and add it to it.

9) What is Divide and Conquer algorithms?


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.

10) Explain the BFS algorithm?


BFS (Breadth First Search) is a graph traversal algorithm. It starts traversing
the graph from the root node and explores all the neighboring nodes. It
selects the nearest node and visits all the unexplored nodes. The algorithm
follows the same procedure for each of the closest nodes until it reaches the
goal state.

Algorithm

Step1: Set status=1 (ready state)

Step2: Queue the starting node A and set its status=2, i.e. (waiting state)

Step3: Repeat steps 4 and 5 until the queue is empty.

Step4: Dequeue a node N and process it and set its status=3, i.e.
(processed state)

Step5: Queue all the neighbors of N that are in the ready state (status=1)
and set their status =2 (waiting state)
[Stop Loop]

Step6: Exit
11) What is Dijkstra's shortest path algorithm?
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.

Suppose you want to go from home to office in the shortest possible way.
You know some roads are heavily congested and challenging to use this,
means these edges have a large weight. In Dijkstra's algorithm, the shortest
path tree found by the algorithm will try to avoid edges with larger weights.

12) 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)

13) What are Greedy algorithms? Give some example of it?


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.

Greedy algorithms find the overall, ideal solution for some idealistic
problems, but may discover less-than-ideal solutions for some instances of
other problems.
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

14) What is a linear search?


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.

For example, suppose an array of with some integer elements. You should
find and print the position of all the elements with their value. Here, the
linear search acts in a flow like matching each element from the beginning of
the list to the end of the list with the integer, and if the condition is `True
then printing the position of the element.'

Implementing Linear Search

Below steps are required to implement the linear search.

Step1: Traverse the array using for loop.

Step2: In every iteration, compare the target value with the current value of
the array

Step3: If the values match, return the current index of the array

Step4: If the values do not match, shift on to the next array element.

Step5: If no match is found, return -1


15) What is a Binary Search Tree?
The binary search tree is a special type of data structure which has the
following properties.

o Nodes which are less than root will be in the left subtree.
o Nodes which are greater than root (i.e., contains more value) will be
right subtree.
o A binary search tree should not have duplicate nodes.
o Both sides subtree (i.e., left and right) also should be a binary search
tree.

16) Write an algorithm to insert a node in the Binary search tree?


Insert node operation is a smooth operation. You need to compare it with the
root node and traverse left (if smaller) or right (if greater) according to the
value of the node to be inserted.
Algorithm:

o Make the root node as the current node


o If the node to be inserted < root
o If it has left child, then traverse left
o If it does not have left child, insert node here
o If the node to be inserted > root
o If it has the right child, traverse right
o If it does not have the right child, insert node here.

17) How to count leaf nodes of the binary tree?


Algorithm-

Steps for counting the number of leaf nodes are:

o If the node is null (contains null values) then return 0.


o If encountered leaf node. Left is null and node Right is null then return
1.
o Recursively calculate the number of leaf nodes using

No. of leaf nodes= no of leaf nodes in left subtree + number of leaf nodes in
the right subtree.

18) How to find all possible words in a board of characters


(Boggle game)?
In the given dictionary, a process to do a lookup in the dictionary and an M x
N board where every cell has a single character. Identify all possible words
that can be formed by order of adjacent characters. Consider that we can
move to any of the available 8 adjacent characters, but a word should not
have multiple instances of the same cell.

Example:
1. dictionary[] = {"Java", "Point","Quiz"};
2. Array[][] = {{'J', 'T', 'P',},
3. {'U', 'A', 'A'},
4. {'Q', 'S', 'V'}};
5. isWord(str): returns true if str is present in dictionary
6. else false.

Output:

Following words of the dictionary are present


JAVA

19) Write an algorithm to insert a node in a link list?


Algorithm

o Check If the Linked list does not have any value then make the node as
head and return it
o Check if the value of the node to be inserted is less than the value of
the head node, then insert the node at the start and make it head.
o In a loop, find the appropriate node after which the input node is to be
inserted. To find the just node start from the head, keep forwarding
until you reach a node whose value is greater than the input node. The
node just before is the appropriate node.
o Insert the node after the proper node found in step 3.

20) How to delete a node in a given link list? Write an algorithm


and a program?
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.

We may assume that the Linked List never becomes empty.

Suppose the function name is delNode(). In a direct implementation, the


function needs to adjust the head pointer when the node to be deleted the
first node.

C program for deleting a node in Linked List

We will handle the case when the first node to be deleted then we copy the
data of the next node to head and delete the next node. In other cases when
a deleted node is not the head node can be handled generally by finding the
previous node.

1. #include <stdio.h>
2. #include <stdlib.h>
3. struct Node
4. {
5. int data;
6. struct Node *next;
7. };
8.
9. void delNode(struct Node *head, struct Node *n)
10. {
11. if(head == n)
12. {
13. if(head->next == NULL)
14. {
15. printf("list can't be made empty because there is only on
e node. ");
16. return;
17. }
18. head->data = head->next->data;
19. n = head->next;
20. head->next = head->next->next;
21. free(n);
22. return;
23. }
24. struct Node *prev = head;
25. while(prev->next != NULL && prev->next != n)
26. prev = prev->next;
27. if(prev->next == NULL)
28. {
29. printf("\n This node is not present in List");
30. return;
31. }
32. prev->next = prev->next->next;
33. free(n);
34. return;
35. }
36. void push(struct Node **head_ref, int new_data)
37. {
38. struct Node *new_node =
39. (struct Node *)malloc(sizeof(struct Node));
40. new_node->data = new_data;
41. new_node->next = *head_ref;
42. *head_ref = new_node;
43. }
44. void printList(struct Node *head)
45. {
46. while(head!=NULL)
47. {
48. printf("%d ",head->data);
49. head=head->next;
50. }
51. printf("\n");
52. }
53. int main()
54. {
55. struct Node *head = NULL;
56. push(&head,3);
57. push(&head,2);
58. push(&head,6);
59. push(&head,5);
60. push(&head,11);
61. push(&head,10);
62. push(&head,15);
63. push(&head,12);
64. printf("Available Link list: ");
65. printList(head);
66. printf("\nDelete node %d: ", head->next->next->data);
67. delNode(head, head->next->next);
68.
69. printf("\nUpdated Linked List: ");
70. printList(head);
71.
72. /* Let us delete the the first node */
73. printf("\nDelete first node ");
74. delNode(head, head);
75.
76. printf("\nUpdated Linked List: ");
77. printList(head);
78.
79. getchar();
80. return 0;
81. }

Output:

Available Link List: 12 15 10 11 5 6 2 3


Delete node 10:
Updated Linked List: 12 15 11 5 6 2 3
Delete first node
Updated Linked list: 15 11 5 6 2 3
21) 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.

Use of extra space is not allowed i.e., insertion must be done in a place.
Predictable time complexity is O(n) where n is number of nodes in first list.

1. #include <stdio.h>
2. #include <stdlib.h>
3. struct Node
4. {
5. int data;
6. struct Node *next;
7. };
8. void push(struct Node ** head_ref, int new_data)
9. {
10. struct Node* new_node =
11. (struct Node*) malloc(sizeof(struct Node));
12. new_node->data = new_data;
13. new_node->next = (*head_ref);
14. (*head_ref) = new_node;
15. }
16. void printList(struct Node *head)
17. {
18. struct Node *temp = head;
19. while (temp != NULL)
20. {
21. printf("%d ", temp->data);
22. temp = temp->next;
23. }
24. printf("\n");
25. }
26. void merge(struct Node *p, struct Node **q)
27. {
28. struct Node *p_curr = p, *q_curr = *q;
29. struct Node *p_next, *q_next;
30. while (p_curr != NULL && q_curr != NULL)
31. {
32. p_next = p_curr->next;
33. q_next = q_curr->next;
34. q_curr->next = p_next;
35. p_curr->next = q_curr;
36. p_curr = p_next;
37. q_curr = q_next;
38. }
39.
40. *q = q_curr;
41. }
42. int main()
43. {
44. struct Node *p = NULL, *q = NULL;
45. push(&p, 3);
46. push(&p, 2);
47. push(&p, 1);
48. printf("I Linked List:\n");
49. printList(p);
50.
51. push(&q, 8);
52. push(&q, 7);
53. push(&q, 6);
54. push(&q, 5);
55. push(&q, 4);
56. printf("II Linked List:\n");
57. printList(q);
58.
59. merge(p, &q);
60.
61. printf("Updated I Linked List:\n");
62. printList(p);
63.
64. printf("Updated II Linked List:\n");
65. printList(q);
66. getchar();
67. return 0;
68. }

Output:

I Linked List:
1 2 3
II Linked List:
4 5 6 7 8
Updated I Linked List:
1 4 2 5 3 6
Updated II Linked List:
7 8

22) Explain how the encryption algorithm works?


Encryption is the technique of converting plaintext into a secret code format
it is also called as "Ciphertext." To convert the text, the algorithm uses a
string of bits called as "keys" for calculations. The larger the key, the higher
the number of potential patterns for Encryption. Most of the algorithm use
codes fixed blocks of input that have a length of about 64 to 128 bits, while
some uses stream method for encryption.

23) What Are The Criteria Of Algorithm Analysis?


An algorithm is generally analyzed by two factors.
o Time complexity
o Space complexity

Time complexity deals with the quantification of the amount of time taken
by a set of code or algorithm to process or run as a function of the amount of
input. In other words, the time complexity is efficiency or how long a
program function takes to process a given input.

Space complexity is the amount of memory used by the algorithm to


execute and produce the result.

24) What are the differences between stack and Queue?


Stack and Queue both are non-primitive data structure used for storing data
elements and are based on some real-world equivalent.

Let's have a look at key differences based on the following parameters.

Working principle

The significant difference between stack and queue is that stack uses LIFO
(Last in First Out) method to access and add data elements whereas Queue
uses FIFO (First in first out) method to obtain data member.

Structure

In Stack, the same end is used to store and delete elements, but in Queue,
one end is used for insertion, i.e., rear end and another end is used for
deletion of elements.

Number of pointers used

Stack uses one pointer whereas Queue uses two pointers (in the simple
case).

Operations performed

Stack operates as Push and pop while Queue operates as Enqueue and
dequeuer.

Variants
Stack does not have variants while Queue has variants like a circular queue,
Priority queue, doubly ended Queue.

Implementation

The stack is simpler while Queue is comparatively complex.

25) What is the difference between the Singly Linked List and
Doubly Linked List data structure?
This is a traditional interview question on the data structure. The major
difference between the singly linked list and the doubly linked list is the
ability to traverse.

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.

You might also like