Coding Questions Asked in Data Science Interviews

Contents

This article was published as part of the Data Science Blogathon.

Introduction

Data structures and algorithms are the integral part that each Machine learning Practitioners must know. Allows programmers to write codes in an optimized way, which is very useful, especially when it comes to very large data sets.

Therefore, every aspiring candidate needs to have a good understanding of the fundamentals. Questions about data structure and algorithm are often asked extensively over several rounds of coding.

So I have created a selected list of 15 popular questions about data structure and algorithms often asked in data science interviews.

Try these questions and evaluate yourself!!

1. Which of the following statements are correct about the 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 each pair of vertices in a tree

(d) Any connected graph that has n vertices and n edges is considered a tree

Answer: [ a, b, c ]

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

2. Which of the following statements is TRUE about tree runs for a given tree?

q2-2020713

(a) The order traversal of the given tree is BDAGECHFI

(b) The Preorder traversal of the given tree is ABDCEGFHI

(c) The Postorder traversal of the given tree is DBGEHIFCA

(d) The first width traversal of the given tree is ABCDEFGHI

Answer: [ a, b, c, d ]

Explanation: Preorder: Root → Left → Right

Organize: Left → Root → Right

Postal order: Left → Right → Root

3. Which of the following statements are TRUE about the binary tree?

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

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

(c) It is a special kind of tree data structure.

(d) The number of nodes that have zero children in any binary tree depends only on the number of nodes with 2 sons

Answer: [ c, d ]

Explanation: In a binary tree, each node can have a maximum 2 sons.

Total number of nodes that have zero children in a binary tree = Total number of nodes that have 2 sons + 1

4. Which of the following statements are correct about the binary search tree (BST)?

(a) The binary search tree is considered a special type of binary tree.

(b) The nodes are arranged in a specific order

(c) Only smaller values ​​in its right subtree

(d) Only higher values ​​in its left subtree

Answer: [ a, b ]

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 a special type 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 the right subtree of each node differs by at least one

(d) In AVL trees, the equilibrium factor of each node is 0 O 1 O -1

Answer: [ a, b, d ]

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

6. Which of the following statements are true about the stack data structure?

(a) Stack is a type of dynamic set

(b) Follow the last in principle, first out (LIFO)

(c) The stack is a non-linear data structure

(d) The INSERT on the stack operation is often referred to as a PUSH

Answer: [ a, b, d ]

Explanation: The 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, the height is 0)

(a) 2

(b) 3

(c) 4

(d) 5

Answer: [ b ]

Explanation: The binary search tree formed is shown below:

coding-quation-2-7540384

8. Suppose in a binary tree, the number of internal nodes that have degree-1 is 9 and the number of internal nodes that have degree-2 is 16. Then, the number of nodes they have 0 children in the binary tree is:

(a) 10

(b) 17

(c) 25

(d) 7

Answer: [ b ]

Explanation: Total number of leaf nodes in a binary tree = Total number of nodes that have 2 sons + 1

9. Which of the following statements is TRUE about the array data structure?

(a) An array is a collection of items that are stored in contiguous memory locations

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

(c) Array is a linear data structure

(d) Accessing the elements of the array takes a constant time

Answer: [ a, c, d ]

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

10. How many of the following statements are TRUE about tree terminology??

(a) In any tree, there can 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) The degree of a tree is the total number of children of any node in a tree.

Answer: [ b, c ]

Suggestion: self-explanatory (tree terminology basics)

11. Choose the correct output for the following sequence of operations in the 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

Answer: [ d ]

Explanation: The stack data structure follows the last-in principle, first out (LIFO).

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 is TRUE about the formation of BST?

(a) The root node in the tree formed 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) The node with the label 20 Has only 1 child

Answer: [ a, b, c ]

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

pasted-image-0-7048557

13. Compare the following in terms of increasing time complexity:

F1(n) = 2North, 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

Answer: [ d ]

Explanation: Comparison of various temporal complexities:

O (1) <O (log (calm)) <O (calm) <O (n1/2) <O (n) <O (nlogn) <O (n2) <O (n3) <0 (nk) <O (2North) <O (nNorth)

14. What is the minimum number of nodes needed to build an AVL tree of height = 3?

(a) 5

(b) 6

(c) 7

(d) 8

Answer: [ c ]

Suggestion: Using the recursive relationship: N (h) = N (h-1) + N (h-2) + 1, with the 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 the 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 ]

Suggestion: self-explanatory (take a small tree example then check the options).

Final notes

Thank you for reading!

Hope you enjoyed the questions and were able to test your knowledge of data structures.

If you liked this and want to know more, visit my other articles on data science and machine learning by clicking on the Link

Feel free to contact me at Linkedin, Email.

Anything not mentioned or do you want to share your thoughts? Feel free to comment below and I'll get back to you.

About the Author

Chirag Goyal

Nowadays, I am pursuing my Bachelor of Technology (B.Tech) in Computer Science and Engineering from Indian Institute of Technology Jodhpur (IITJ). I am very excited about machine learning, deep learning and artificial intelligence.

The media shown in this article is not the property of DataPeaker and is used at the author's discretion.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.