Data Structures & Algorithms quiz and interview questions
Data structures are the backbone of coding interviews and computer science exams. This quiz covers stacks, queues, linked lists, trees, hashing and Big-O, with short C++ snippets you have to trace by hand.
Data Structures & Algorithms: key concepts
- Stack and queue
- A stack is Last-In-First-Out (push/pop at the top), used for undo, recursion and parsing. A queue is First-In-First-Out, used for scheduling and BFS.
- Linked list
- Nodes hold data and a pointer to the next node. Insert/delete at a known node is O(1), but finding an element is O(n). Always handle the empty list and the last node.
- Binary search tree
- Left subtree < node < right subtree. Search, insert and delete are O(log n) when balanced, O(n) when it degrades into a line. In-order traversal visits values in sorted order.
- Hash table
- A hash function maps a key to a bucket for O(1) average lookups. Collisions are handled by chaining or open addressing; the worst case is O(n).
- Big-O
- Describes how running time grows with input size: O(1) constant, O(log n) binary search, O(n) one loop, O(n log n) good sorting, O(n²) nested loops.
- Graph traversal
- BFS uses a queue and finds the shortest path in an unweighted graph. DFS uses a stack (or recursion) and is used for cycles and topological order.
All 14 Data Structures & Algorithms questions with answers
Try each one first, then open the answer. Making a class test? Use the test maker.
A stack follows which order?
- A. FIFO
- B. LIFO
- C. Random
- D. Sorted
Show answer
B. LIFO — Last-In-First-Out: the last item pushed is the first one popped, like a stack of plates.
Which data structure is First-In-First-Out?
- A. Stack
- B. Queue
- C. Tree
- D. Heap
Show answer
B. Queue — A queue removes items in the order they were added, like a line at a counter.
What is the time complexity of reading arr[i] from an array?
- A. O(n)
- B. O(log n)
- C. O(1)
- D. O(n²)
Show answer
C. O(1) — Arrays store elements next to each other, so the address of element i is calculated directly — constant time.
Binary search runs in O(log n) but requires the data to be…
- A. Unique
- B. Sorted
- C. In a linked list
- D. Of even length
Show answer
B. Sorted — Binary search halves the search range each step by comparing with the middle element, which only works if the array is sorted.
What does this print?
stack<int> s; s.push(1); s.push(2); s.push(3); s.pop(); cout << s.top();- A. 1
- B. 2
- C. 3
- D. Error
Show answer
B. 2 — After pushing 1, 2, 3 the top is 3. pop() removes 3, so the new top is 2.
What does this print?
queue<int> q; q.push(10); q.push(20); q.push(30); q.pop(); cout << q.front();- A. 10
- B. 20
- C. 30
- D. Error
Show answer
B. 20 — pop() removes the front element (10), so the new front is 20.
Which traversal of a binary search tree visits the values in sorted order?
- A. Pre-order
- B. In-order
- C. Post-order
- D. Level-order
Show answer
B. In-order — In-order visits left subtree, node, right subtree — which in a BST is ascending order.
Values 50, 30, 70, 20, 40 are inserted into an empty BST in that order. What does an in-order traversal print?
- A. 50 30 70 20 40
- B. 20 30 40 50 70
- C. 20 40 30 70 50
- D. 50 30 20 40 70
Show answer
B. 20 30 40 50 70 — In-order traversal of any BST gives the values sorted ascending: 20 30 40 50 70. (50 30 20 40 70 would be pre-order.)
What are the average and worst-case lookup times in a hash table?
- A. O(1) and O(1)
- B. O(1) and O(n)
- C. O(log n) and O(n)
- D. O(n) and O(n²)
Show answer
B. O(1) and O(n) — With a good hash function lookups are O(1) on average. If many keys collide into one bucket, it degrades to O(n).
What's wrong with this loop that should print every node?
Node* cur = head; while (cur->next != NULL) { cout << cur->data << " "; cur = cur->next; }- A. Nothing
- B. It skips the last node (and crashes if head is NULL)
- C. It prints in reverse
- D. Infinite loop
Show answer
B. It skips the last node (and crashes if head is NULL) — The loop stops when cur is the last node, before printing it. Use while (cur != NULL), which also handles an empty list safely.
Which traversal finds the shortest path (fewest edges) in an unweighted graph?
- A. DFS
- B. BFS
- C. In-order
- D. Dijkstra is required
Show answer
B. BFS — BFS explores level by level using a queue, so the first time it reaches a node is via the fewest edges. Dijkstra is for weighted graphs.
In a binary min-heap, what are the costs of insert and of reading the minimum?
- A. O(1) and O(n)
- B. O(log n) and O(1)
- C. O(n) and O(log n)
- D. O(log n) and O(log n)
Show answer
B. O(log n) and O(1) — Insert bubbles the new item up the tree height (log n). The minimum is always at the root, so reading it is O(1). Removing it is O(log n).
What is quicksort's worst-case time complexity?
- A. O(n log n)
- B. O(n)
- C. O(n²)
- D. O(log n)
Show answer
C. O(n²) — If the pivot is always the smallest or largest element (e.g. sorted input with a first-element pivot), partitions are unbalanced and it becomes O(n²). Average is O(n log n).
What happens when fact(5) is called?
int fact(int n) { return n * fact(n - 1); }- A. Returns 120
- B. Returns 0
- C. Infinite recursion until the stack overflows
- D. Compile error
Show answer
C. Infinite recursion until the stack overflows — There is no base case, so it never stops calling itself. Add: if (n <= 1) return 1;
More subjects
Need software for your business instead? I'm an AI automation & full-stack developer — see what I build.