Updated June 16, 2023
Introduction to Algorithm Interview Questions and Answers
Preparing for a job interview in Algorithm. I am sure you want to know the most common 2023 Algorithm Interview Questions and Answers to help you quickly crack the Algorithm Interview. Below is the list of top Algorithm Interview Questions and answers at your rescue.
Below is the list of 2023 Algorithm Interview Questions and answers, which can be asked during an Interview for freshers and more experience. These top interview questions are divided into two parts as follows:
Part 1 – Algorithm Interview Questions and Answers (Basic)
This first part covers basic Interview Questions and Answers.
1. Write an algorithm to reverse a string. For example, if my string is “vahbunA,” my result will be “Anubhav.”
Answer:
- Step 1: Start
- Step 2: Take two-variable I and j.
- Step 3: j is positioned on the last character (Technically, we can do this by length(string)-1)
- Step 4: I is positioned on the first character (we can do this by the string[0])
- Step 5: String[i] is interchanged String[j]
- Step 6: Increment I by 1
- Step 7: Increment J by 1
- Step 8: If ‘I’> ‘j,’ then go to Step 3
- Step 9: Stop
2. Write an algorithm to insert a node in the linked list, assuming the linked list is sorted already?
Answer:
Case 1: If the linked list is empty, return the node as head.
Code:
New_node-> Next = head;
head = New_node
Case 2: Insert node in the middle
Code:
While (P!= insert_position)
{
P= p->Next;
}
Store_next= P->Next;
P->Next = New_Node;
New_Node->Next = Store_next;
Case 3: Insert a node at the end
Code:
While (P->next!= null)
{
P= P->Next;
}
P->Next = New_Node;
New_Node->Next = null;
3. Write an algorithm for bubble sort?
Answer: We will implement the bubble sort algorithm through C language.
Step 1: Repeat Step 2 and Step 3
for I = 1 to 10
Step 2:
Set j = 1
Step 3: Repeat
while j <= n (Where n is number of elements in the array)
{ If a[i] < a[j]
Then interchange a[i] and a[j]
[End of if]
}
Set j = j + 1
[End of inner loop]
[End of step 1 outer loop]
Step 4:
Exit
4. Write an algorithm for Heapsort?
Answer:
- Step 1: Since the tree satisfies the max-Heap property, the essential item is stored at the root node.
- Step 2: Remove the root element and put at the end of the array (nth position) put the last item of the tree (heap) at the vacant place.
- Step 3: Reduce the heap’s size by 1 and heap the root element again to have the highest element at the root.
- Step 4: The process is repeated until all the items on the list are sorted.
Part 2 – Algorithm Interview Questions and Answers (Advanced)
Let us now have a look at the advanced Interview Questions.
5. Write an algorithm for the Fibonacci search?
Answer:
Step 1: A is
sorted_int_array;
Step 2: take one variable c
Step 3:
Fib2 = 1, Fib1 = 1 and fib = 2
Step 4:
While fib < n do (where n is number of element in list)
Step 5: Assign the variable.
Fib2 = Fib1
Fib1 = Fib
Fib = Fib1 + Fib2
End while
Step 6: Assign the value to a temporary variable
I = 0, offset = 0;
Step 7:
While Fib > 1 do
I = min (offset + Fib2, n)
If c < A[i] then
Fib = Fib2
Fib1 = Fib1 – Fib2
Fib2 = Fib – Fib1
Else if c > A[i] then
Fib = Fib1;
Fib1 = Fib2;
Fib2 = Fib – Fib1;
Offset = I;
Else
Return true
End if
End while
Return false
6. Write an algorithm of push and pop operation in the stack?
Answer:
For Push Operation
Procedure Add( Item, Stack, N, Top)
(Insert ‘Item’ into the ‘stack’ of maximum size’ n,’ top is the number of elements currently in ‘Stack’)
Step 1: Check Stack is overflowing.
If (Top >= N)
Stack is overflow
Exit
Step 2: If the stack does not overflow, increment the loop
Top = Top + 1
Step 3: Insert the element.
Stack [Top] = Item
Step 4:
Exit
For POP Operation:
Step 1: Check Stack is underflow means empty.
If (Top <= 0)
Stack is empty
Exit
Step 2: If the stack is not underflow, then delete the element
Item = stack[top]
Step 3: decrementing the top value
Top = Top – 1
Step 4:
Exit
7. Write an algorithm for inserting and deleting operations in the queue?
Answer:
For insertion operation
Procedure add(queue, F, R, N, item)
(This will insert ‘item’ in the ‘queue’ after ‘R’ (rare) where ‘n’ is the array size.)
Step 1: Check Queue is overflow means the queue is full.
If (R >= N)
Queue is full
Exit
Step 2: If the Queue is not overflowed, then increment the loop
R = R + 1
Step 3: Insert an element into the queue
Queue[R] = item
Step 4: Setting the ‘F’ (Front) Pointer
If ( F = 0)
F = 1
Exit
For Deletion operation in the queue
Procedure delete(queue, F, R, item)
(Delete ‘item’ from the ‘stack,’ ‘F’ is the Front-end pointer, and ‘R’ is the rare end pointer.
Step 1: Check Queue is underflow means empty.
If ( R < = 0)
Queue is empty
Exit
Step 2: Deleting an element from the queue
Item = queue [F]
Step 3: Incrementing the value of F
F = F+1
Step 4: Checking the empty queue
If (F > R)
Then F = R = 0
Exit
8. Write an algorithm to find the minimum depth of a binary tree?
Answer:
Let “node” be the pointer to the root node of a subtree.
- Step 1: If the node is equal to Null, return 0
- Step 2: If the node is a leaf node, return 1.
- Step 3: Recursively, find the minimum depth of the left and right sub-tree; let it be left Min Depth and right min depth, respectively.
- Step 4: To get the tree’s minimum height rooted at the node, we will take a minimum of left min depth and right min depth and 1 for the root node.
Program:
Procedure minDepth ( Node )
Step 1:
if ( root = null)
Return 0
Step 2:
if (root -> Left = Null and root -> right = Null )
Return 1
Step 3:
if (root -> left is not null)
Return minDepth (root -> right ) + 1;
Step 4:
If (root -> Right is not null )
Return minDepth ( root -> left) + 1;
Step 5:
return min(minDepth (root -> left), minDepth (root -> right)) + 1
Recommended Articles
This has been a comprehensive guide to the Algorithm Interview Questions and answers so that the candidate can easily crack down on these Algorithm Interview Questions. You may also look at the following articles to learn more –