## 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 to convert a binary tree into heap.

The following algorithm to convert a binary tree into heap:

- Set i=(n-1)/2
- Set j=2i+1
- Let heap=False
- Compare the value at index j with the value of its sibling at index j+1:
- If A[j+1]>A[j]:
- j=j+1

- If A[j]>A[(j-1)/2]:
- If((n>j) or(heap=False)),repeat step 4,5, and 6
- Decrement i by 1
- If(i>=),repeat steps 2 through 8

Swap A[j] with A[(j-1)/2

j=2j+1

Else

heap=True

## To represent an arithmetic expression binary tree uses three notations which are those notations?

To represent an arithmetic expression binary tree uses three notations those are:

- Infix notation
- Prefix notation
- Postfix notation

## Write an algorithm to convert a binary tree into heap.

The following algorithm to convert a binary tree into heap:

- Set i=(n-1)/2
- Set j=2i+1
- Let heap=False
- Compare the value at index j with the value of its sibling at index j+1:
- If A[j+1]>A[j]:
- j=j+1

- If A[j]>A[(j-1)/2]:
- If((n>j) or(heap=False)),repeat step 4,5, and 6
- Decrement i by 1
- If(i>=),repeat steps 2 through 8

Swap A[j] with A[(j-1)/2

j=2j+1

Else

heap=True

## Write an algorithm for preorder traversing of binary tree.

Algorithm: Preorder(root)

- If(root=NULL):Exit
- Visit(root)
- Preorder(left child of root)
- Preorder(right child of root)

## Consider the following tree and answer the questions that follow.

- What is the depth of the tree?
- Which nodes are the children of node B?
- Which node is the parent of node F?
- What is the level of node E
- Which nodes are the siblings of node H?
- Which nodes are the siblings of node D?
- Which nodes are the leaf nodes?

**Answers:**

- 4
- D and E
- C
- 2
- H does not have any siblings
- The only sibling of D is E
- F,G,H and I

## What are the various application fields where binary search trees are used?

Some of them are as follows:

- Indexing
- Encoding messages using Huffman tree
- Arithmetic expression evaluation
- Sorting data by using Heap tree

## Write an algorithm to insert a node in a binary search tree.

The following is the algorithm to insert a node in a binary search tree:

- Allocate memory for the new node.
- Assign value to the data field of new node.
- Make the left and right child of the new node point to NULL.
- Locate the node that will be the parent of the node to be inserted. Mark it as parent. Execute the following steps to locate the parent of the new node to be inserted:
- Mark the root node as currentNode
- Make parent point to NULL
- Repeat steps ,e,and f,until currentNode becomes NULL
- Make parent point to currentNode
- If the value of the new node is less than that of currentNode:
- Make currentNode point to its left child

- If the value of the new node is greater than that of currentNode:
- Make currentNode point to its right child

- If parent is NULL(Tree is empty):
- Mark the new node as ROOT
- Exit

- If the value in the data field of the new node is less than that of parent:
- Make the left child of parent point to the new node
- Exit

- If the value in the data field of the new node is greater than that of the parent:
- Make the right child of parent point to the new node
- Exit

## Definitions

How do you define terms:

- Siblings/Brothers
- Internal node
- Level of a node
- Depth of a tree

- Siblings/Brothers: Children of the same node are called siblings of each other. In the preceding figure:
- Nodes 7 and 3 are siblings of each other
- Nodes 2 and 6 are siblings of each other

- Internal Node: An intermediate node between the root and the leaf nodes is called an internal node. It is also referred to as a non terminal node. In preceding figure, nodes 7,3,6 and 9 are internal nodes
- Level of a node: The distance of a node from the root is called the level of a node. The root always lies at level 0. As move down the tree, the level of a node increases in such a way that if a node is at level n, then its children are at level n+1. In preceding figure, level of node 1 is 0, level of nodes 7 and 3 are 1 and so on.
- Depth of a tree: The maximum number of levels in a tree is called the depth of a tree. In other words, depth of a tree is one more than maximum level of the tree. The depth of a tree in above figure is 4

## How can a binary tree be represented as a *array*?

Consider the binary tree with the nodes labeled from 0 to 6,as shown in the following figure.

In the preceding binary tree, the node labeled x will be stored at index x in the array. Consider the array representation of preceding binary tree, as shown in the following figure.

For any node with index i, n-1>i>0,the following holds true:

- Parent of i is at index(i-1)/2
- Left child of i is at index 2i+1. If 2i+1>n-1 there is no left child.
- Right child of i is at index 2i+2. If 2i+2>n-1 there is no right child

## What are steps for postorder traversing a binary tree?

The steps for post order traversing a binary tree are as follows:

- Traverse the left subtree
- Traverse the right subtree
- Visit the root

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