# Popular Coding Questions asked in Data Science Interviews

## Introduction

Data Structures and Algorithms are the integral part that every Machine Learning Practitioners should know about.

It allows the programmers to write codes in an optimized manner which comes in handy to use especially when we are dealing with very large datasets.

Therefore it becomes necessary for every aspiring candidate to have a good grasp of the fundamentals. Data Structure and Algorithm Questions are usually asked extensively in multiple coding rounds.

So I have come up with a curated list of 15 popular Data Structure and Algorithm questions often asked in Data Science Interviews.

Do Attempt these questions and evaluate yourself!

### 1. Which of the following statements are correct about Tree Data Structure?

(a) It is a non-linear data structure

(b) In a tree data structure, a node can have any number of child nodes

(c) There is one and only one possible path between every pair of vertices in a tree

(d) Any connected graph having n vertices and n edges is considered as a tree

Answer: [ a, b, c ]

Explanation: A graph is a tree if and only if it is minimally connected, which means any connected graph with n vertices and (n-1) edges is a tree.

### 2. Which of the following statements are TRUE about Tree Traversals for a given tree?

(a) The Inorder traversal of the given tree is B D A G E C H F I

(b) The Preorder traversal of the given tree is A B D C E G F H I

(c) The Postorder traversal of the given tree is D B G E H I F C A

(d) The breadth-first traversal of the given tree is A B C D E F G H I

Answer: [ a, b, c, d ]

Explanation: Preorder: Root → Left → Right

Inorder: Left → Root → Right

Postorder: Left → Right → Root

### 3. Which of the following statements are TRUE about Binary Tree?

(a) In a binary tree, each node must have 2 children

(b) In a binary tree, nodes are always arranged in a specific order

(c) It is a special type of tree data structure

(d) Number of nodes having zero children in any binary tree depends only on the number of nodes with 2 children

Explanation: In a binary tree, each node can have at most 2 children.

Total Number of  nodes having zero children in a Binary Tree = Total Number of nodes having 2 children + 1

### 4. Which of the following statements are correct about Binary Search Tree(BST)?

(a) Binary Search Tree is considered as a special type of binary tree

(b) Nodes are arranged in a specific order

(c) Only smaller values in its right subtree

(d) Only larger values in its left subtree

Explanation: In a binary search tree (BST), each node contains, only smaller values in its left subtree and only larger values in its right subtree.

### 5. Which of the following statements are TRUE about AVL Tree?

(a) AVL trees are considered as a special kind of binary search tree

(b) AVL trees are also called self-balancing binary search trees

(c) In AVL trees, the height of the left subtree and right subtree of every node differs by at least one

(d) In AVL trees, the balancing factor of each node is either 0 or 1 or -1

Answer: [ a, b, d ]

Explanation: In AVL trees, the height of the left subtree and the right subtree of every node differs by at most one.

### 6. Which of the following statements are True about Stack Data Structure?

(a) Stack is a type of dynamic set

(b) It follows the Last-In-First-Out(LIFO) principle

(c) Stack is a non-linear Data Structure

(d) The INSERT operation on the stack is often known as PUSH

Answer: [ a, b, d ]

Explanation: Stack is a linear Data Structure.

7. The following integers are inserted into an initially empty binary search tree in order:

10, 1, 3, 5, 15, 12, 16

### What is the height of the binary search tree formed? (Here, Height is defined as the maximum distance of a leaf node from the root. If the tree has only the root node then the height is 0)

(a) 2

(b) 3

(c) 4

(d) 5

Explanation: The Binary Search Tree formed is shown as below:

### 8. Suppose in a Binary tree, the number of the internal nodes having degree-1 is 9 and the number of internal nodes having degree-2 is 16. Then, the number of nodes having 0 children in the binary tree are:

(a) 10

(b) 17

(c) 25

(d) 7

Explanation: Total Number of leaf nodes in a Binary Tree = Total Number of nodes having 2 children + 1

### 9. Which of the following statements are TRUE about Array Data Structure?

(a) An array is a collection of elements that are stored at contiguous memory locations

(b) Array can store the elements of different data type

(c) Array is a Linear Data Structure

(d) Accessing array elements takes constant time

Answer: [ a, c, d ]

Explanation: Array contains all the elements of the same data type.

### 10. How many of the following statements are TRUE about Tree Terminology?

(a) In any tree, there may be more than one root node

(b) The connecting link between any two nodes in a tree is called an edge

(c) Nodes that belong to the same parent are called siblings

(d) Degree of a Tree is the total number of children of any node of a tree

Hint: Self Explanatory(Basics of Tree Terminology)

### 11.Choose correct output for the following sequence of operations on Stack Data Structure:

```push(5)
push(8)
pop
push(2)
push(5)
pop
pop
pop
push(1)
pop```

(a) 8 5 5 2 1

(b) 8 2 5 5 1

(c) 8 1 2 5 5

(d) 8 5 2 5 1

Explanation: Stack Data Structure follows the Last-In-First-Out(LIFO) Principle.

### 12. A binary search tree is formed by inserting the numbers in the given order-

50, 5, 20, 58, 91, 3, 8, 24

Then, Which of the following statements are TRUE about BST formed?

(a) The root node in the formed tree is 50

(b) Number of nodes in the left subtree of the root = 5

(c) Number of nodes in the right subtree of the root = 2

(d) Node with label 20 have only 1 child

Answer: [ a, b, c ]

Explanation: The tree formed after inserting all the elements is shown as below:

### 13. Compare the following in the term of increasing time complexity:

f1(n)=2n,   f2(n)=n3/2,   f3(n)=nlog2n,   f4(n)=nlog2n

(a) f2, f3, f4, f1

(b) f2, f1, f3, f4

(c) f1, f2, f3, f4

(d) f3, f2, f4, f1

Explanation: Comparison of various time complexities:

O(1) <O(log(logn)) <O(logn) <O(n1/2) <O(n) <O(nlogn) <O(n2) <O(n3) <0(nk) <O(2n) <O(nn)

### 14. What is the minimum number of nodes required to construct an AVL Tree of height = 3?

(a) 5

(b) 6

(c) 7

(d) 8

Hint: Using the recursive relation: N(h) = N(h-1) + N(h-2) + 1, with base condition as N(0)=1 and N(1)=2 and here we have to calculate the value of N(3).

### 15. Which of the following properties are correct about Binary Tree?

(a) Minimum number of nodes in a binary tree of height H = H + 1

(b) Maximum number of nodes in a binary tree of height H = 2H+1 – 1

(c) Maximum number of nodes at any level ‘L’ in a binary tree = 2L

(d) Maximum number of nodes at any level ‘L’ in a binary tree = 2L-1

Answer: [ a, b, c ]

Hint: Self Explanatory(Take a small tree example and then verifies the options).

## End Notes 