All subjectsComputer science · 14 questions · 6 junior / 6 mid / 2 senior

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 quiz14 questions in the bank · 10 per round
Level
Question type
Mode

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.

  1. juniorConcepts

    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.

  2. juniorConcepts

    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.

  3. juniorConcepts

    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.

  4. midConcepts

    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.

  5. juniorPredict the output

    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.

  6. juniorPredict the output

    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.

  7. midConcepts

    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.

  8. midPredict the output

    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.)

  9. midConcepts

    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).

  10. midFind the bug

    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.

More subjects

Need software for your business instead? I'm an AI automation & full-stack developer — see what I build.

Chat on WhatsApp