## Interview and Exam Questions and Answers

This set contains a list of commonly asked questions. They are short interview questions aimed at freshers interview, campus placement drives, and also for job interviews. You can use these to have a quick grasp and brush-up of your fundamentals. These questions can be viewed on a mobile phone also because this website is built on responsive web design.

## Write an algorithm for bubble sort.

- Set pass=1
- Repeat step 3 varying j from 0 to (n - 1 - pass).
- If the element at index j is greater than the element at index j+1, swap the two elements.
- Increment pass by 1.
- If (pass <= n="" -="" 1)="" go="" to="" step="">

## What is worst case and average case of bubble sort algorithm?

Worst case = O(n^{2}) and average case = O(n^{2}).

## How do we define bubble sort?

Bubble sort repeatedly scan the list, comparing adjacent elements swapping them if they are in wrong order.## Write an algorithm to search for an employee ID in an array(Hint: use linear search)

- Read the employee ID to be searched
- Set i=0
- Repeat step d until i>n or arr[i]=employee ID
- Increment i by 1
- If i>n : Display "Not found"

Else Display "Found"

## Write an algorithm fo deleting a node between two nodes in the doubly-linked list.

The algorithm to delete a node between two nodes in the list is as follows:

- Make the node to be deleted as current and its predecessor as previous. To locate previous and current, execute the following steps:
- Make previous point to NULL.
- Make current point to the first node in the linked list.
- Repeat steps d and e until either the node is found or current becomes NULL.
- Make previous point to current.
- Make current point to the next node in sequence.

- Make the next field of previous point to the successor of current
- Make the prev field of the successor of current point to previous
- Release the memory of the node marked as current.

## Write an algorithm to delete a node from the end of the circular -linked list.

The algorithm to delete a node from the end of the circular -linked list:

- Make current point to LAST.
- mark the predecessor of LAST as previous. To locate the predecessor of LAST execute the following steps:
- Make previous point to the successor of LAST.
- Repeat step c until the successor of previous becomes LAST.
- Make previous point to the next node in sequence.

- Make the next field of previous point to successor of LAST.
- Mark previous as LAST.
- Release the memory of node marked as current.

## What is best case performance of bubble sort algorithm?

The best case performance of bubble sort algorithm is O(n).

## Write an algorithm to insert a node between two nodes in the list, when the list is not empty.

If the list is not empty, the following algorithm is used to insert a node between two nodes in the linked list.

- Identify the nodes between which th new node is to be inserted. Mark them as previous and current. To locate previous and current, execute the following steps:
- Make current point to the first node.
- Make previous point to NULL.
- Repeat step d and e until current.info>newnode.info or previous=LAST
- Make previous point to current.
- Make current point to the next node in sequence.

- Allocate a memory fo new node.
- Assign a value to the data field of the new node.
- Make the next field of new node point to current.
- Make the next field of previous point to the new node.

## Write Code for bubble sort in java

public void bubbleSort(int[] arr) { boolean swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; j++; for (int i = 0; i < arr.length - j; i++) { if (arr[i] > arr[i + 1]) { tmp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = tmp; swapped = true; } } } }

## Draw the structure of a linked list.

first part represent data and second part represent address of next node

## My C/C++ Videos on Youtube

Here is the complete playlist for video lectures and tutorials for the absolute beginners. The language has been kept simple so that anybody can easily understand them. I have avoided complex jargon in these videos.