# Data Structure & Algorithms Interview Questions/Answers

In this article, we are going to see the top most important questions and answers about Data Structures and algorithms. so if you are preparing for competitive exams, or for coding interviews for big companies like Google, Microsoft, Amazon, etc then you must prepare for the Data structures and algorithms questions. So here we are including some **basic** to **advanced questions** related to **DSA** so you can prepare for this topic.

## Consider the following tree

If the post-order traversal gives ab-cd*+ then the label of the nodes 1,2,3,… will be

- +,-,*,a,b,c,d
- a,-,b,+,*,d
- a,b,c,d,d,-,*,+
- -,a,b,+,*,c,d

**Answer – **(1) +,-,*,a,b,c,d

## Consider the following tree

If this tree is used for sorting, then a new number 8 should be placed as the

- the left child of the node labeled 30
- right child of the node labeled 5
- right child of the node labeled 30
- the left child of the node labeled 10

**Answer – **(4) the left child of the node labeled 10

## The initial configuration of a queue is a,b,c,d,(‘a’ is in the front end). To get the configuration d,c,b, a, one needs a minimum of

- 2 deletions and 3 additions
- 3 deletions and 2 additions
- 3 deletions and 3 additions
- 3 deletions and 4 additions

**Answer – **(3) 3 deletions and 2 additions

## The order of a B-tree is defined as the

- Minimum no. of keys in a non-root node
- Maximum no. of keys in a non-root node
- (n + 1) / 2
- Maximum no. of pointers in any node

**Answer – **(4) Maximum no. of pointers in any node

## If the sequence of operations – push(1), push(2), pop, push(1), push(20), pop, pop, pop, push(2), pop, are performed on a stack, the sequence of popped out values are

- 2,2,1,1,2
- 2,2,1,2,2
- 2,1,2,2,1
- 2,1,2,2,2

**Answer – **(1) 2, 2, 1, 1, 2

## The depth of a complete binary tree with ‘n’ nodes is (log is to the base two)

- log(n + 1) – 1
- log(n)
- log(n – 1) + 1
- log(n) + 1

**Answer – **(1) log(n + 1) – 1

## Preorder is the same as

- depth-first order
- breadth-first order
- topological order
- linear order

**Answer – **(1) depth-first order

## Which of the following traversal techniques lists the nodes of a binary search tree in ascending order?

- Post-order
- In-order
- Pre-order
- None of the above

**Answer – **(2) In-order

## A hash function f defined as f(key) = key mod 7, with linear probing, is used to insert the keys 37,38,72,48,98,11,56, into a table indexed from 0 to 6. What will be the location of key 11?

- 3
- 4
- 5
- 6

**Answer –** (3) 5

## There are 4 different algorithms A1, A2, A3, and A4 to solve a given problem with the order log(n), log(log(n)), nlog(n), and n/logn respectively. Which is the best algorithm?

- A1
- A2
- A4
- A3

**Answer – **(2) A2

## The number of possible binary search trees with 3 nodes is

- 12
- 13
- 5
- 15

**Answer – **(3) 5

## Sorting is useful for

- report generation
- minimizing the storage needs and responding to queries easily
- making searching easier and more efficient
- all of the above

**Answer – **(4) all of the above

## The order of an algorithm that finds whether a given Boolean function of ‘n’ variables, produces a 1 is

- constant
- linear
- logarithmic
- exponential

**Answer – **(4) exponential

## In evaluating the arithmetic expression 2 * 3 – (4 + 5), using stacks to evaluate its equivalent postfix form, which of the following stack configuration is not possible?

**Answer – **(d)

## The way a card game player arranges his cards as he picks them up one by one is an example of

- bubble sort
- selection sort
- insertion sort
- merge sort

**Answer – **(3) insertion sort

## You want to check whether a given set of items is sorted. Which of the following sorting methods will be the most efficient if it is already in sorted order?

- Bubble sort
- Selection sort
- Insertion sort
- Merge sort

**Answer – **(3) insertion sort

## Which of the following sorting methods will be the best if a number of swappings are done, is the only measure of efficiency?

- bubble sort
- selection sort
- insertion sort
- Quicksort

**Answer – **(3) insertion sort

## You are asked to sort 15 randomly generated numbers. You should prefer

- bubble sort
- selection sort
- insertion sort
- Quicksort

**Answer – **(3) insertion sort

## As part of maintenance work, you are entrusted with the work of rearranging the library books on a shelf in proper order, at the end of each day. The ideal choice will be

- bubble sort
- selection sort
- insertion sort
- heap sort

**Answer – **(2) selection sort

## Which of the following algorithms exhibits the unnatural behavior that, the minimum number of comparisons are needed if the list to be sorted is in the reverse order and a maximum number of comparisons are needed if they are already in sorted order?

- Heapsort
- Radix sort
- Binary insertion sort
- There can’t be any such sorting methods

**Answer – **(3) Binary insertion sort

Note – more questions and answers will be added from time to time.