## 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.

## Give some examples where binary search could be used?

The examples of binary search are:

- Number guessing game
- Word lists
- Applications to complexity theory

## Is this code for bubble sort or not ?

void DoBubbleSort () { int s,t, numItems = 10; for(s=(numItems - 1); s >=0; s--) { for(t=1;t<=s;t++) { if(arr[t-1]>arr[t]) { temp=arr[t-1]; arr[t-1]=arr[t]; arr[t]=temp; } } } }Yes, the code is for bubble sort.

## Write pseudo code to search the array in the reverse order, returning 0 when the element is not found.

- Set i to n.
- Repeat this loop
- If i <= 0,="" then="" exit="" the="">
- If A[i] = x, then exit the loop.
- Set i to i - 1.

- Return i

## How singly-linked list is represented in a program?

A linked list is represented in a program by defining two classes:

a) Node class: A node is the basic building block in a linked list.The*Node*class represents a node in a linked list

b) List class: This class consists of a set of operations implemented on a linked list. These operations are insertion, deletion, search, and traversal.

## What is the average case performance of selection sort?

The average case performance of selection sort is O(n^{2}).

## Write an algorithm for deleting a node from the beginning of the doubly linked list.

The algorithm to delete a node from the beginning of the list is as follows:- Mark the first node in the list as current.
- Make START point to the next node in its sequence.
- If START is not NULL:
- Assign NULL to the prev field of the node marked as START.

- Release the memory of the node marked as current.

## What simple modification could be made to the bubble sort algorithm that would make it perform more efficiently on the case described above?

The algorithm could be made to start at the end of the list and move towards the front, swapping elements only if the first is greater than the second. In this way, the smallest numbers would bubble down instead of the large numbers bubbling up. Note however that this algorithm has its own worst case scenario that is just as bad as the worst case for the normal bubble sort. It occurs when the list's largest element is in the first position.

## Give an example of Turtle problem in bubble sort.

Though, array {2, 3, 4, 5, 1} is almost sorted, it takes O(n^{2}) iterations to sort an array. Element {1} is a turtle.

## Write algorithm for binary search for a desired element.

The following algorithm is used for binary search:

- Accept the element to be searched
- Set lowerbound=0
- Set upperbound=n-1
- Set mid=(lowerbound+upperbound)/2
- If arr[mid]=desired element:
- Display "Found"
- Go to step 10

- If desired element<arr[mid]:
- Set upperbound=mid-1

- If desired element>arr[mid]:
- Set lowerbound=mid+1

- If lowerbound ≤upperbound:
- Go to step 4

- Display "Not Found"
- Exit

## Write an algorithm for traversing a singly-linked list

The algorithm for traversing a singly-linked list is as follows:

- Make currentNode point to the first node in the list.
- Repeat step 3 and 4 until currentNode becomes NULL.
- Display the information contained in the node marked as currentNode.
- Make currentNode point to thr next node in sequence.

## 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.