Data Structures Quiz - Languages Python
Free practice quiz: Data Structures (Languages Python). 42 multiple-choice questions with answers and explanations. Play online, no sign-up needed.
Play all 42 questions freeSample questions
Consider a singly linked list where each node contains an integer value. If you need to insert a new node at the beginning of the list, what is the time complexity of this operation?
- O(1)
- O(n)
- O(log n)
- O(n^2)
Show answer
Answer: A. O(1)
Inserting a node at the beginning of a singly linked list involves creating the new node, setting its 'next' pointer to the current head, and then updating the head to point to the new node. These operations are constant time, regardless of the list's size.
What is the worst-case time complexity for searching an element in an unsorted array?
- O(1)
- O(log n)
- O(n)
- O(n^2)
Show answer
Answer: C. O(n)
In an unsorted array, to guarantee finding an element (or determining it's not present), you might have to examine every single element in the worst case, leading to a linear time complexity of O(n).
What is the maximum number of comparisons required to insert an element into a binary search tree with 'n' nodes?
- O(1)
- O(log n)
- O(n)
- O(n log n)
Show answer
Answer: C. O(n)
In the worst case, if the binary search tree is skewed (degenerate), inserting an element might require traversing all 'n' existing nodes. In a balanced BST, insertion takes O(log n) time. The question asks for the maximum, implying the worst case.
What is the primary advantage of using a Trie (prefix tree) over a hash table for string storage and retrieval, especially when dealing with prefix searching?
- Lower memory usage for all cases
- Faster average lookup time for all operations
- Efficient prefix searching and auto-completion capabilities
- Guaranteed O(1) insertion time
Show answer
Answer: C. Efficient prefix searching and auto-completion capabilities
Tries are specifically designed for efficient prefix searching, auto-completion, and spell checking. Traversing the Trie based on a prefix directly leads to all strings that share that prefix. While hash tables are good for exact matches, they are not inherently optimized for prefix-based queries.
Which of the following is a key property of a min-heap?
- The parent node's value is always greater than or equal to its children's values.
- The parent node's value is always less than or equal to its children's values.
- The smallest element is always at a leaf node.
- All elements are stored in sorted order.
Show answer
Answer: B. The parent node's value is always less than or equal to its children's values.
In a min-heap, the heap property ensures that the value of a parent node is always less than or equal to the values of its children. This guarantees that the minimum element is always at the root of the heap.