In this article, we are going to see about the top most important questions and answers about the Data Structures and algorithms. so if you are preparing for the 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.

Data Structure & Algorithms Interview Questions and Answers

Consider the following tree

Data Structure & Algorithms Interview Questions and Answers
If the post order traversal gives ab-cd*+ then the label of the nodes 1,2,3,... will be
  1. +,-,*,a,b,c,d
  2. a,-,b,+,*,d
  3. a,b,c,d,d,-,*,+
  4. -,a,b,+,*,c,d
Answer - (1) +,-,*,a,b,c,d

Consider the following tree

Data Structure & Algorithms Interview Questions and Answers


If this tree is used for sorting, then a new number 8 should be placed as the
  1. the left child of the node labeled 30
  2. right child of the node labeled 5
  3. right child of the node labeled 30
  4. 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

  1. 2 deletions and 3 additions
  2. 3 deletions and 2 additions
  3. 3 deletions and 3 additions
  4. 3 deletions and 4 additions
Answer - (3) 3 deletions and 2 additions

The order of a B-tree is defined as the

  1. Minimum no. of keys in a non-root node
  2. Maximum no. of keys in a non-root node
  3. (n + 1) / 2
  4. 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 performad on a stack, the sequence of popped out values are

  1. 2,2,1,1,2
  2. 2,2,1,2,2
  3. 2,1,2,2,1
  4. 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)

  1. log(n + 1) - 1
  2. log(n)
  3. log(n - 1) + 1
  4. log(n) + 1
Answer - (1) log(n + 1) - 1

Preorder is same as

  1. depth-first order
  2. breadth-first order
  3. topological order
  4. linear order
Answer - (1) depth-first order

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

  1. Post-order
  2. In-order
  3. Pre-order
  4. 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?

  1. 3
  2. 4
  3. 5
  4. 6
Answer - (3) 5

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

  1. A1
  2. A2
  3. A4
  4. A3
Answer - (2) A2

The number of possible binary search trees with 3 nodes is

  1. 12
  2. 13
  3. 5
  4. 15
Answer -  (3) 5

Sorting is useful for

  1. report generation
  2. minimizing the storage needs and responding to queries easily
  3. making searching easier and more efficient
  4. 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

  1. constant
  2. linear
  3. logarithmic
  4. 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?
Data Structure & Algorithms Interview Questions and Answers

Answer - (d)

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

  1. bubble sort
  2. selection sort
  3. insertion sort
  4. 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?

  1. Bubble sort
  2. Selection sort
  3. Insertion sort
  4. Merge sort
Answer - (3) insertion sort

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

  1. bubble sort
  2. selection sort
  3. insertion sort
  4. Quicksort
Answer - (3) insertion sort

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

  1. bubble sort
  2. selection sort
  3. insertion sort
  4. 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

  1. bubble sort
  2. selection sort
  3. insertion sort
  4. 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?

  1. Heapsort
  2. Radix sort
  3. Binary insertion sort
  4. 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.