Algorithms & Data Structures
Computer science notes on Big-O, arrays, stacks, queues, trees and sorting algorithms with examples.
Last updated: 23 November 2026
Key points
- An algorithm is a finite list of well-defined steps that solves a problem. Efficiency is described with Big-O notation, which shows how running time grows with input size n: O(1) constant, O(log n) binary search, O(n) linear search, O(n log n) merge sort and O(n²) bubble sort.
- Arrays give O(1) access by index but O(n) insertion in the middle. Linked lists give O(1) insertion at the head but O(n) search.
- A stack is Last-In-First-Out (push and pop), used for undo features and function calls. A queue is First-In-First-Out (enqueue and dequeue), used in scheduling.
- A binary search tree keeps left child < node < right child, so searching is O(log n) when the tree is balanced. Hash tables offer O(1) average lookup.
- Merge sort is O(n log n) in the worst case. Quicksort is O(n log n) on average but O(n²) in the worst case.
Worked example
Worked example: binary search for 23 in [2, 5, 8, 12, 16, 23, 38]
The middle element is 12 and 23 > 12, so search the right half [16, 23, 38]. Its middle element is 23, which is found. Only 2 comparisons were needed instead of 6.
Practice MCQs
Q1. Which data structure works on a Last-In-First-Out basis?
- Queue
- Stack
- Array
- Graph
Show answer
Answer: B. Stack. A stack removes the most recently added item first.
Q2. What is the time complexity of binary search?
- O(n)
- O(log n)
- O(n²)
- O(1)
Show answer
Answer: B. O(log n). Binary search halves the search range each step, so it takes O(log n).
Q3. Which data structure is First-In-First-Out?
- Stack
- Queue
- Tree
- Array
Show answer
Answer: B. Queue. A queue serves items in the order they arrived.
Q4. What is the worst-case time complexity of bubble sort?
- O(n)
- O(n log n)
- O(n²)
- O(log n)
Show answer
Answer: C. O(n²). Bubble sort compares pairs repeatedly, which takes O(n²) in the worst case.
Q5. Which operation is usually O(1)?
- Accessing an array element by index
- Linear search
- Bubble sort
- Merge sort
Show answer
Answer: A. Accessing an array element by index. Indexing jumps straight to the position, so the time does not depend on n.
Q6. Which data structure is natural for an undo feature?
- Queue
- Stack
- Tree
- Hash table
Show answer
Answer: B. Stack. Undo reverses the most recent action first: Last-In-First-Out.
Q7. Which sorting algorithm is O(n log n) on average but O(n²) in the worst case?
- Merge sort
- Quicksort
- Bubble sort
- Heap sort
Show answer
Answer: B. Quicksort. A bad pivot choice can make quicksort O(n²).
Q8. Binary search requires the data to be:
- Sorted
- Unique
- Numeric only
- In a linked list
Show answer
Answer: A. Sorted. Halving the range only works when the data is in order.
Q9. What is the average time for a hash table lookup?
- O(n)
- O(log n)
- O(1)
- O(n²)
Show answer
Answer: C. O(1). A good hash function finds the slot directly.
Q10. Which structure stores each item with a pointer to the next item?
- Array
- Linked list
- Matrix
- Bitmap
Show answer
Answer: B. Linked list. Each node of a linked list holds data and a link to the next node.
Q11. Which queue operation adds an item?
- Push
- Pop
- Enqueue
- Peek
Show answer
Answer: C. Enqueue. Enqueue adds at the back, dequeue removes from the front.
Q12. In a binary search tree, values in the left subtree are ___ the node’s value.
- greater than
- less than
- equal to
- unrelated to
Show answer
Answer: B. less than. BST rule: left < node < right.
Q13. Which running time grows fastest as n becomes large?
- O(n)
- O(log n)
- O(n²)
- O(n log n)
Show answer
Answer: C. O(n²). n² grows faster than n log n, n or log n.
Q14. An algorithm must be:
- Infinite
- Finite and well-defined
- Random
- Written in Python
Show answer
Answer: B. Finite and well-defined. An algorithm is a finite list of well-defined steps.
Helpful tools
ILOMRA can open full notes and practice MCQs for almost any study topic. Search any topic on ILOMRA.