Data Structures & Algorithms Interview Questions
Comprehensive data structures & algorithms interview questions and answers for Python. Prepare for your next job interview with expert guidance.
Questions Overview
1. What is the time complexity of dictionary operations in Python?
Basic2. How does a list differ from an array in Python?
Basic3. Explain the implementation and use cases of heapq in Python.
Moderate4. What is the difference between sorted() and sort() methods?
Basic5. How do you implement a binary search tree in Python?
Advanced6. What are deques and when should they be used?
Moderate7. How does the bisect module work in Python?
Moderate8. Explain how sets handle hash collisions in Python.
Advanced9. What is the implementation of sorting algorithms in Python's list.sort()?
Advanced10. How do you implement a LinkedList in Python?
Moderate11. What are OrderedDict and defaultdict use cases?
Moderate12. How do you implement graph algorithms in Python?
Advanced13. What is the difference between list and tuple for storing data?
Basic14. How do you implement dynamic programming solutions in Python?
Advanced15. What are the methods to handle collisions in hash tables?
Advanced16. How do you implement a stack and queue using list?
Basic17. What is the Counter class and how is it used?
Moderate18. How do you implement a trie (prefix tree) in Python?
Advanced19. What are the benefits of using bytearray over bytes?
Moderate20. How do you implement binary search in Python?
Moderate21. What are the differences between list methods append() and extend()?
Basic22. How do you implement a priority queue in Python?
Advanced23. What are the performance implications of string concatenation?
Moderate24. How do you implement union-find (disjoint set) in Python?
Advanced25. What are the differences between range and xrange?
Basic26. How do you implement memoization in Python?
Moderate27. What are the best practices for dictionary key selection?
Moderate28. How do you implement sorting algorithms like quicksort and mergesort?
Advanced29. What are itertools functions and their use cases?
Moderate1. What is the time complexity of dictionary operations in Python?
BasicAverage case time complexities for dictionary operations are: Get item O(1), Set item O(1), Delete item O(1), Search O(1). However, worst case can be O(n) due to hash collisions. Dictionary uses hash table implementation for efficient access.
2. How does a list differ from an array in Python?
BasicLists are dynamic arrays that can store elements of different types. Arrays (from array module) store homogeneous data, are more memory efficient, and support mathematical operations. Lists offer more flexibility but use more memory due to storing references.
3. Explain the implementation and use cases of heapq in Python.
Moderateheapq implements min heap, providing O(log n) push/pop operations. Used for priority queues, finding n smallest/largest elements, and scheduling tasks. Methods include heappush(), heappop(), heapify(). Can implement max heap by negating values.
4. What is the difference between sorted() and sort() methods?
Basicsorted() creates new sorted list, leaving original unchanged. list.sort() modifies list in-place. Both use Timsort algorithm, accept key function and reverse parameter. sorted() works on any iterable, while sort() is list-only method.
5. How do you implement a binary search tree in Python?
AdvancedImplement using class with Node class (value, left, right pointers) and Tree class with insert, delete, search methods. Balance tree using rotations. Time complexity O(log n) average case, O(n) worst case for unbalanced tree.
6. What are deques and when should they be used?
ModerateDeques (collections.deque) are double-ended queues supporting O(1) append/pop at both ends. Used for sliding windows, maintain last n items, implement queues/stacks efficiently. More efficient than lists for these operations.
7. How does the bisect module work in Python?
Moderatebisect module provides binary search functionality for sorted sequences. bisect_left finds insertion point for element to maintain sort order, bisect_right finds rightmost position. O(log n) time complexity. Useful for maintaining sorted sequences.
8. Explain how sets handle hash collisions in Python.
AdvancedSets use hash tables with open addressing to handle collisions. When collision occurs, probing finds next empty slot. Load factor determines resize threshold. Set operations (union, intersection) are optimized using hash-based algorithms.
9. What is the implementation of sorting algorithms in Python's list.sort()?
AdvancedPython uses Timsort, hybrid of merge sort and insertion sort. Stable sort with O(n log n) worst case. Adaptive algorithm that performs well on real-world data with partially ordered sequences. Minimizes comparisons using galloping mode.
10. How do you implement a LinkedList in Python?
ModerateCreate Node class with value and next pointer. LinkedList class manages head pointer, implements insert, delete, traverse methods. Optional: add tail pointer for O(1) append. Consider implementing iterator protocol for traversal.
11. What are OrderedDict and defaultdict use cases?
ModerateOrderedDict maintains insertion order (pre-3.7), useful for LRU caches. defaultdict provides default values for missing keys, simplifying handling of nested structures and counters. Both from collections module.
12. How do you implement graph algorithms in Python?
AdvancedRepresent graphs using adjacency lists (dictionaries) or matrices. Implement BFS/DFS using queue/stack. Use dict/set for visited nodes. Consider weight handling for Dijkstra/shortest paths. Implement path finding and cycle detection.
13. What is the difference between list and tuple for storing data?
BasicTuples are immutable, slightly more memory efficient, can be dictionary keys. Lists are mutable, support item assignment, have more methods. Tuples often used for returning multiple values, representing fixed collections.
14. How do you implement dynamic programming solutions in Python?
AdvancedUse memoization (decorator or dict) or tabulation (arrays). Handle base cases, build solution using smaller subproblems. Consider space optimization using rolling arrays. Common in optimization problems.
15. What are the methods to handle collisions in hash tables?
AdvancedPython uses open addressing with random probing. Alternative methods: chaining (linked lists), linear probing, quadratic probing. Load factor determines rehashing. Performance depends on hash function quality and collision resolution.
16. How do you implement a stack and queue using list?
BasicStack: use append() and pop() for LIFO. Queue: use append() and pop(0)/list.pop(0) for FIFO (inefficient, use collections.deque instead). Consider implementing size limits and empty checks.
17. What is the Counter class and how is it used?
ModerateCounter from collections creates dictionary subclass for counting hashable objects. Supports addition, subtraction, intersection, union. Methods: most_common(), elements(). Useful for frequency counting and multisets.
18. How do you implement a trie (prefix tree) in Python?
AdvancedCreate TrieNode class with children dict and is_end flag. Implement insert, search, startswith methods. Use dict for child nodes. Time complexity O(m) for operations where m is key length. Useful for autocomplete, spell check.
19. What are the benefits of using bytearray over bytes?
Moderatebytearray is mutable version of bytes. Supports in-place modifications, useful for building binary data incrementally. Methods like append(), extend(), reverse(). Consider for large binary data manipulation.
20. How do you implement binary search in Python?
ModerateImplement iteratively or recursively on sorted sequence. Handle mid calculation, comparison, and boundary updates. Time complexity O(log n). Consider bisect module for built-in implementation. Handle edge cases and duplicates.
21. What are the differences between list methods append() and extend()?
Basicappend() adds single element, extend() adds elements from iterable. append([1,2]) adds list as single element, extend([1,2]) adds individual elements. extend() equivalent to += operator. Consider memory and performance implications.
22. How do you implement a priority queue in Python?
AdvancedUse heapq module for min heap implementation. Wrap elements in tuples with priority. Alternative: queue.PriorityQueue for thread-safe version. Operations: push O(log n), pop O(log n). Handle custom comparison.
23. What are the performance implications of string concatenation?
ModerateString concatenation with += creates new string objects. Use join() method for multiple concatenations, more efficient. For building strings, consider list of strings or io.StringIO. String interning affects memory usage.
24. How do you implement union-find (disjoint set) in Python?
AdvancedImplement find and union operations using dictionary/array. Use path compression and union by rank optimizations. Time complexity nearly O(1) with optimizations. Used in Kruskal's algorithm, connected components.
25. What are the differences between range and xrange?
BasicIn Python 2, range creates list, xrange creates iterator object. Python 3's range is like xrange, memory efficient iterator. Use for loops, sequence generation. Consider memory usage for large ranges.
26. How do you implement memoization in Python?
ModerateUse decorator with cache dict, or functools.lru_cache. Store function arguments as keys. Handle mutable arguments. Consider cache size limits and cleanup. Used in dynamic programming, expensive computations.
27. What are the best practices for dictionary key selection?
ModerateUse immutable types (strings, numbers, tuples of immutables). Consider hash collisions and distribution. Custom objects need __hash__ and __eq__. Avoid mutable keys that could change hash value.
28. How do you implement sorting algorithms like quicksort and mergesort?
AdvancedQuicksort: partition around pivot, recursively sort subarrays. Mergesort: divide array, sort recursively, merge sorted halves. Consider in-place vs new array, stability requirements, pivot selection strategies.
29. What are itertools functions and their use cases?
Moderateitertools provides efficient iteration tools: combinations(), permutations(), product(), cycle(), chain(). Memory efficient iterators for combinatorial operations. Used in generating test cases, processing sequences.