CCS Data Structures and Algorithms 2 — Questions and Answers
Question 1: Which data structure follows the LIFO (Last In, First Out) principle?
- Queue
- Stack (Correct answer)
- Linked List
- Tree
Correct answer: Stack
A stack operates on LIFO, where the last element added is the first one removed.
Question 2: What is the time complexity of accessing an element in an array by index?
- O(n)
- O(log n)
- O(1) (Correct answer)
- O(n²)
Correct answer: O(1)
Array index access is O(1) because elements are stored at contiguous memory locations.
Question 3: In a binary search tree (BST), which traversal produces nodes in sorted ascending order?
- Pre-order
- Post-order
- In-order (Correct answer)
- Level-order
Correct answer: In-order
In-order traversal (left, root, right) visits BST nodes in ascending sorted order.
Question 4: What is the worst-case time complexity of quicksort?
- O(n log n)
- O(n)
- O(n²) (Correct answer)
- O(log n)
Correct answer: O(n²)
Quicksort degrades to O(n²) when the pivot is consistently the smallest or largest element.
Question 5: Which data structure is best suited for implementing a breadth-first search (BFS)?
- Stack
- Queue (Correct answer)
- Heap
- Hash Table
Correct answer: Queue
BFS uses a queue to process nodes level by level in FIFO order.
Question 6: What does a hash table use to map keys to their storage locations?
- Sorting function
- Hash function (Correct answer)
- Binary search
- Pointer arithmetic
Correct answer: Hash function
A hash function converts a key into an index used to store and retrieve values in O(1) average time.
Question 7: Which sorting algorithm has the best average-case time complexity for large datasets?
- Bubble sort
- Selection sort
- Insertion sort
- Merge sort (Correct answer)
Correct answer: Merge sort
Merge sort consistently achieves O(n log n) in all cases, making it optimal for large datasets.
Which data structure follows the LIFO (Last In, First Out) principle?