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

## While implementing the bubble sort algorithm, how many comparisons will performed in Pass 1?

n - 1 comparisons.

## How do you define selection sort?

Selection sort scans through the list iteratively, selecting one item in each scan, and moving the item to its correct position in the list.

NOTE: Iteratively means one by one. It can also be called 'in a loop'.

## Write an algorithm to delete a node from the beginning of a circular list.

The algorithm to delete a node from beginning of the list:

- If the node to be deleted is the only node in the list:
- Mark LAST as NULL
- Exit

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

## Define these Terms.

How do you define terms:

- Leaf node
- Subtree
- Children of a node
- Degree of a node
- Edge

- Leaf Node:Anode with no children is called leaf node. They are also referred as terminal nodes. In the given figure,2,5,11 and 4 are leaf nodes
- Subtree: A portion of a tree, which can be viewed as tree in itself,is called subtree. In preceding figure the tree starting at node 7, containing nodes 2 and 6.A subtree can also contain just one node called the leaf node. In other words, all the leaf nodes are subtrees but all the subtrees are not leaf nodes
- Children of a node: The roots of the subtrees of a node are called the children of the node. In figure 7 and 3 are children of node 1.
- Degree of a node: The number of subtrees of a node is called the degree of a node.in given :
- Degree of node 1 is 2
- Degree of node 7 is 2
- Degree of node 3 is 1

- Edge: A link from the parent to the child node is referred to as edge. It is also known as a branch. A tree with n nodes has n-1 edges.

## Which are the various variants of binary tree?

Various variants of binary tree are:

- Strictly binary tree: A binary tree is said to be strictly binary if every node, except for the leaf nodes, have non-empty left and right children. A strictly binary tree is shown in following figure:

In the preceding binary tree, every non-leaf node has precisely two children. Therefore, it is strictly binary tree. - Full binary tree: A binary tree of depth d is said to be a full binary tree if it has exactly 2
^{d-1} - Complete binary tree: A binary tree with n nodes and depth d is complete if and only if its nodes correspond to the nodes numbered from 0 to n-1 in the full binary tree of depth k. A complete binary tree is shown in the following figure:

## Sort the following array

{ 30, 50, 20, 10, 40 }

### Answer:

- We find the smallest element, starting from index 0:
{ 30, 50, 20,

**10**, 40 } - We then swap this with the element at index 0:
{

**10**, 50, 20,**30**, 40 } - Now that the first element is sorted, we can ignore it. Consequently, we find the smallest element, starting from index 1:
{ 10, 50,

**20**, 30, 40 } - And swap it with the element in index 1:
{ 10,

**20**,**50**, 30, 40 } - Find the smallest element starting at index 2:
{ 10, 20, 50,

**30**, 40 } - And swap it with the element in index 2:
{ 10, 20,

**30**,**50**, 40 } - Find the smallest element starting at index 3:
{ 10, 20, 30, 50,

**40**} - And swap it with the element in index 3:
{ 10, 20, 30,

**40**,**50**} - Finally, find the smallest element starting at index 4:
{ 10, 20, 30, 40,

**50**} - And swap it with the element in index 4 (which doesn't do anything):
{ 10, 20, 30, 40,

**50**} - Done!

## Write Code for bubble sort in C++

void bubbleSort(int arr[], int n) { bool swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; j++; for (int i = 0; i < n - j; i++) { if (arr[i] > arr[i + 1]) { tmp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = tmp; swapped = true; } } }

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

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

## How can a binary tree be represented as a *linked list*?

## What do you mean by * Binary tree*?

A binary tree is a specific type of tree in which each node can have a maximum of two children. These child nodes are typically distinguished as the left and the right child. The structure of a binary tree is shown in the following figure:

In the preceeding binary tree,node B is the left child of node A, and node C is the right child of node A. Similarly, node D is the left child of node B,and node E is the right child of node B.

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