Python Data Structures is an important topic in every Python based Interviews hence it is absolutely necessary for a candidate to have complete understanding of the questions and answers that can be asked from this topic. In this article, I have gone through all the possible Python Data Structures questions based on my experience. Hopefully this will help you crack all kinds of Python based Interviews.
Python Data Structures Interview Questions and Answers
1. What are different data types in Python Collection ?
Ans. There are four different types:-
2. Does tuple collection type allows duplicate values ?
3. Which is an unordered and unindexed collection type in Python ?
4. Does Set allows duplicate values ?
5. Which is an ordered and changeable collection type in Python ?
6. Does Dictionary Collection Types allows duplicate value ?
7. Can we change values in Tuple Collection Type ?
8. Which function can be used to determine the number of members in tuple collection type ?
Ans. len() function
Ans. It is stored in key value pair.
10. Which is an unordered and changeable collection type in Python ?
11. Which method is used to update the items in Python Dictionary ?
Ans. update() method
12. Which method can be used to add items from another set to current set ?
Ans. update() method
13. Which method can be used to add an item to the Set ?
Ans. add() method
14. Which method can be used to remove an item from List ?
Ans. remove() method
15. Which method can be used to remove an item from a specified index in List ?
Ans. pop() method
16. Which method can be used to delete all the List values without deleting the List itself ?
Ans. clear() method
17. Can we delete the List by using del keyword ?
18. Which method can be used to insert value at the specified Index in the List ?
Ans. insert() method
19. Which method can be used to add the values at the end of the List ?
Ans. extend() method
20. Which method can count the number of occurrences of a value in a tuple ?
Ans. count() method
21. What is insert operations called in queue ?
22. What is delete operations called in queue ?
23. Does queue allow for random access to the objects ?
24. Does List allow for random access to the objects ?
25. Does Array allow for random access to the objects ?
26. What is the difference between PriorityQueue and Heapq ?
Ans. PriorityQueue is synchronized and provides locking mechanism to support multiple concurrent producers and consumers whereas heapq does not.
27. When should you use heapq over the PriorityQueue ?
Ans. If you are looking to avoid the locking overhead of PriorityQueue then heapq is the best in the situation.
28. What is the main difference between Array and Tuples ?
Ans. Array stores all the elements of same data type which makes it tightly packed and hence provides the efficient utilization of storage whereas in the case of List, it stores values of different data types which makes the data loosely packed and hence does not provide efficient utilization of storage.
29. What is the main difference between byteArray and bytes ?
Ans. byteArray can be modified and changed whereas bytes object cannot.
30. Which data structure would be more suitable to store numeric data with tight packing ?
31. Which data structure would be more suitable to store data of different data types ?
Ans. List or Tuple
32. Which Immutable Data Structure would be more suitable to store Contiguous block of bytes ?